Creating Custom Events with JavaScript: Decoupling:
Many “Web 2.0″ applications suffer from too-tight-coupling between the various javascript objects used to model the data and control the interface. This has always been seen as a necessary evil because there seemed to be no good alternative.
One common solution in desktop applications is called “event driven programming.” Not just user-supplied events like mouse clicks, hovers, and window scrolls (obviously, we do all of that in our javascript apps already), but actual data-driven or state-driven events such as “message selected” or “preference changed”.
Truly custom events, when used correctly, allow you to decouple many of the JS objects in your application. This leads to better self-containment and much better maintainability.
It has generally been considered impossible — or at least too difficult — to create truly custom events in our applications. There is a W3C spec for custom events, but it’s not supported by most of the browsers so is really of no use to us.
I’ve been playing around with this a little bit over the last few days and I have to say that it is cool, beyond cool it is more of a duh… of course this is how things should work. It’s really clever and works well in my limited world (I am fortunate to work mostly on intranet projects that have a very limited scope when it comes to supporting browsers. My guess is that we will see this article or others like it gaining some real traction (also see: http://www.dustindiaz.com/custom-events/ for another take from the Dojo side of the road.)
I am sure I will have more to say on this later as I get a better handle on it and put some ideas into some production environments. Or if Seth bugs me some more….
(Via TruerWords.)
FYI: My article is mostly YUI driven, and not dojo driven. It’s my public duty to make that clear
Oops corrected