Skip to content

Additions to the Motionbox Eventhandler

by Topper on July 29th, 2008

I’ve been working on some additions to the Motionbox EventHandler. It hasn’t moved to master yet, but you can check out the defer_functions branch.

The two major additions are:

  1. The ability to subscribe to Objects (rather than just css rules)
  2. The ability to defer firing of functions (using a setTimeout)

[js]
var someObj = {
foo: ‘bar’
};

MBX.EventHandler.subscribe(someObj, “customFunc”, function () { alert(‘hi’); });
MBX.EventHandler.fireCustom(someObj, “customFunc”, { myCustomPayload: ‘hi’ });
[/js]

Since DOM elements are simply objects, you can also use the eventhandler to subscribe “the old fashioned way.”
[js]
MBX.EventHandler.subscribe(document, “click”, function () { alert(‘hi’); });
[/js]

You can defer the firing of functions (say you don’t care that a function execute as soon as an event is fired).
[js]
MBX.EventHandler.subscribe(document, “dom:loaded”, function () { alert(‘hi’); }, { defer: true });
[/js]

Speaking of domready, you can now subscribe to DOM ready events:
[js]
MBX.EventHandler.onDomReady(function() { alert(‘hi’); });
[/js]