[jQuery] Custom Events in jQuery

[jQuery] Custom Events in jQuery


After developing a rather complex JavaScript application I found
myself starting to get frustrated with the rather limited set of
events that JavaScript itself defines. As the project progressed it
became more dependant on multiple objects, all of which had to be as
self-contained as humanly possible, but which also needed to
communicate with each other. I was thinking about using a form full
of hidden fields that existed solely to have events fired on them for
inter-object communication, but after reading http://www.dustindiaz.com/custom-events/
I thought that maybe the idea of custom events would be a better
approach.
This got me thinking, Yahoo provide a library to handle custom events,
but at the moment there isn't a jQuery equivalent that I know of.
I've started wondering how difficult it would be to implement a system
of custom events on top of the jQuery event system. After reading the
article I started imagining something like this.
$.event.register ('collapse');
to register a new event, and
$.event.trigger ('collapse');
to fire it.
Then for anything that needs to listen to that event you'd use the
normal syntax except with the name of your custom event in place of
the standard ones.
$('.myTreeView').collapse (function (e){console.log (e);});