Create listener to behavior

Is there currently a way to create a Listener to a behaviour in code?

You would think you could use “HypeTriggerCustomBehavior” but nope…

I would have thought that too :slight_smile:. The next release will have this; sample code:

	function myCallback(hypeDocument, element, event) {
		alert("custom behavior triggered: " + event.customBehaviorName);
	}
	
	if("HYPE_eventListeners" in window === false) {
		window.HYPE_eventListeners = Array();
	}
	window.HYPE_eventListeners.push({"type":"HypeTriggerCustomBehavior", "callback":myCallback});
6 Likes

Thanks @jonathan,

This now works as you say.

On thing you may want to do when using this is to put your main code for the call back in a normal Hype function and use the callback to target that.

    function myCallback1(hypeDocument, element, event) {
		hypeDocument.functions().trig(hypeDocument, element, event)
	}
	
	if("HYPE_eventListeners" in window === false) {
		window.HYPE_eventListeners = Array();
	}
	window.HYPE_eventListeners.push({"type":"HypeTriggerCustomBehavior", "callback":myCallback1});

And in the Hype function

	 if (event.customBehaviorName === 'TestCustom'){
     alert("custom behavior triggered: " + event.customBehaviorName);
  }
3 Likes