Skip to content

Custom JavaScript events using the Motionbox EventHandler

by Topper on April 22nd, 2008

YUIBlog has a very nice writeup on using custom events to build components.

The Motionbox EventHandler will offer the same capabilities they talk about to the Prototype developer.

The idea here is that you very loosely couple all your components and fire custom events between them to communicate. You do not register callbacks on individual components, rather individual components broadcast interesting events. If another component is interested in that event, it just listens to it.

So:

JavaScript:
  1. MBX.EventHandler.subscribe(".pod", "podShown", function (evt) { alert('i should be doing good stuff: ' + evt.iCanBeCustomData); });
  2. //Then when that pod gets shown:
  3. MBX.EventHandler.fireCustom($("podElementWithClassOfPod"), "podShown", {iCanBeCustomData: 'anything you want'});

Nothing really has to know about anything else and you can start to assemble your components to form a larger app without worrying about stepping on each others toes (except for event naming conventions).

From → Social Web