Create listener to behavior


#1

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


(Mark Hunte) #2

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


(Jonathan Deutsch) #3

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});

(Mark Hunte) #4

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);
  }