Latest: buggy behaviour of parent:: in PHP 5.3.3

Content with Style

Web Technique

JS custom events roundup

by Pascal Opitz on November 2 2008, 20:06

With independent UI components, that not always know about each other, custom events come in handy. They enable us to code components that will populate hooks for other objects that then can be used to trigger different behaviours. Even better, the various objects that do pick up on our custom events don't necessarily have to be anticipated, but can subscribe to the provided events at any point throughout the development process. The observer pattern is a good example for this.

But with different implementations in IE and Firefox to start with, scripting you own custom events hasn't been easy from the start.

Thankfully, a while ago some folk did good work in wrapping up the two implementations into a single class for custom events.

Nowadays, most major javascript toolkits have some sort of custom event facility.

From YUI I am familiar with the CustomEvents class, which enables us to create custom events, then fire them or subscribe to them

var foo = new YAHOO.util.CustomEvent('foo event');
foo.subscribe(function(type, args){}, true);;

The way JQuery uses custom events is by attaching custom events with the bind function to a DOM element wrapped in the $ function, which then can be triggered:

$('#foo').bind('bar_event', function(event, param){});

I didn't read up on this yet, but I am sure that other toolkits like prototype, dojo and mootools will have similar event models at hand.

Further reading


Leave your comment

Comments are moderated.
Tags allowed: a, strong, em, code, ul, ol, li, q, blockquote, br, p