Reglib gets support for submit / change / select / reset event delegation
By greimer on Dec 05, 2008
Anybody who's ever implemented or used event delegation knows first-hand the bitter pain of IE's lack of bubbling on those events. Bitter, bitter pain.
Well, the reglib trunk has just received
experimental beta support for those events. The technique is of course to co-opt other events that \*do\* bubble, and that are almost certain to be executed prior to the non-bubbling event. Within the co-opted, bubbling event, the non-bubbling event handler is added.
The fly in the ointment is the "almost" in the above phrase, "almost certain." At the very least, these events can be triggered programatically, without any prior events firing. But I thought what the heck, might as well build it and see how useful it is in the real world.
Of course many browsers do support bubbling on these events, so they get reglib's natural event delegation. Although I'll probably need a more sophisticated test,
if(document.all&&!window.opera) seems a bit crude, but I wanted to get this into the trunk and in front of people. I'm sure I'll wake up in a cold sweat tonight and realize I need to recode the whole darn thing.
Here are the new methods:
reg.submit(selector,function); reg.reset(selector,function); reg.select(selector,function); reg.change(selector,function);
Update: So far the results are pretty promising, better than I had hoped even. Early iterations of the code were a bit unweildy and bug-prone, then I had an epiphany and about 90% of the code and complexity evaporated, leaving a solution that seems pretty tight, altogether.
Basically, it sets up reglib's existing delegation on the fly, directly on the element that receives the non-bubbling event. E.g. for the change event, it more or less does
reg.focus('select',setDelegationOnThis) to ensure the delegation gets set up prior to a change event. Of course this is only done on IE. Everything bubbles normally for modern browsers. They are quite boring, those modern browsers.