Sending message to Hype Document from a nested (child) Hype Document


(steve) #1

I’ve got a Hype Document and within that is nested another Hype Document - inside a rect element of the parent Hype Document… I’d like to trigger actions in the parent Hype Document from the child. I don’t want to jump to another scene, or start a timeline. I want to have different javascript functions within the parent Hype Document get triggered by what buttons the user clicks on in the child Hype Document.

What would be the best way to go about doing that?


#2

Include the script in both Head-HTML and then you can send messages from one Hype-Document to the other. Read the instructions. Have fun.


(steve) #3

Wow. What a great project. Thank you so much Max! I got it to work for a parent child situation as I described above, where a Hype document (parent) has a rect element that contains an iframe containing another Hype document (child). They are able to send messages back and forth, however, when the parent sends a message to the child, it get repeated four times. It has something to do with the for loop where it’s checking through the ids of the documents. However, It works fine for the child sending a message to the parent.
Here’s an example project I’ve worked on. The parent and child both send each other messages, through an inline button and also through a Hype function via a Hype button. Each has a behavior that receives the messages and passes it along to a function, which announces that it has received a message.

If you can spot what is causing it to repeat the message from the parent to the child, that would be much appreciated.


#4

At first glance I see that your sending messages throught HypeGlobalBehavior.triggerCustomBehavior() but actually should be using hypeDocument.triggerCustomBehavior() the difference being that the on case triggers all children the other excludes the sending child in receiving as it know from how the message come (hypeDocument).

Try that and when I am back from meetings this afternoon I will look into it in detail.


#5

BTW: the mentioned HypeGlobalBehavio.trigger… is for usage from outside the hype ecosystem. So from a Page Level. But illl look into it later if it’s a bug or just wrong applied commands.


#6

BTW: Saw you copy & pasted the code into the head and linked to it?
Update: I will make sure that the code runs as a singleton in the future


#7

Apart of the Singelton issue (meaning preventing that if somebody by accident includes the code multiple times to avoid miss configuration) I found the bug…

Will provide a fix this afternoon. One if-statment was missing :slight_smile:

Thanks for the catch


#8

Updated to version 1.5 —> Download newest version here
1.5 Fixed a bug with iFrame propagation and added a "Singleton" check

Hints:
Make sure to not include it multiple times but if you still do so by accident at least the code shouldn’t register twice anymore ( because of the new Singleton)… Also using HypeGlobalBehaviour.triggerCustomBehaviour or hypeDocument.triggerCustomBehaviour shouldn’t make a difference …either works.

Regards and let me know if it work for you now.

Another Hint:
GlobalBehavior actually extends the internal Hype behavior. So every possibility you got to send a behavior from within Hype will be distributed over all Hype widgets/iFrames. Using the @ symbol is also not a must… it only makes sure that only a specific Hype widgets/iFrames gets the message. Just adding this as I haven’t made enough documentation or examples … yet.


(steve) #9

Thank you Max! That seems to be working just fine now. I forget to remove the file I had added. Good that you put in the error checking too. I originally added your Javascript file to Hype, into the library like I do for other Javascript libraries. For some reason, that didn’t work, so I pasted the code into the Head section. Should it work the same if you add it to the library instead of pasting the code into the Head?


#10

Yes, just look at the newly added section installation on the Project-Page.
Have fun and let me know if you need further assistance. Also I am about to add a great new feature next week when I return from the holidays.