Friday Dec 05, 2008

Reglib gets support for submit / change / select / reset event delegation

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);

More info: all reglib posts, reglib feed, download reglib


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.

Monday Dec 01, 2008

reglib 1.0.6 released

UPDATE: I've removed reg.pause() and reg.resume() from the trunk. Sean Hogan makes a good point in the comments below. Plus, now that reglib is doing delegation on form events, things get complicated maintaining the pause/resume system alongside the branch of code that simulates bubbling for form events in IE. One can still easily disable an event by using a selector like this: "body.on a@href" and then adding/remove 'on' from the body classname as necessary.


reglib has reached 1.0.6. Updates include:

reg.pause() and reg.resume() follow a similar pattern to reg.removeEvent():

// handle hover on all links
var hoverLinks = reg.hover('a@href',function(e){
    // handle mouseenter
},function(e){
    // handle mouseleave
});

// stop handling hover on links
reg.pause(hoverLinks);

// resume handling hover on links
reg.resume(hoverLinks);

More info: all reglib posts, reglib feed, download reglib

Tuesday Nov 25, 2008

Goodbye mouseover, hello mouseenter

I decided that mouseover/out is never the behavior I want, whereas mouseenter/leave is always the behavior I want. Practically always. So I've changed over reglib's reg.hover() to behave like mouseenter/leave.

[Read More]

Sunday Nov 23, 2008

reglib 1.0.4 released

reglib is up to 1.0.4. Some bugs are fixed and some features are added since the 1.0 release:

Here's an example of removeEvent():

var el = reg.gebi('myid');

// mem is just an id
// similar to that returned by setTimeout()
var mem = reg.addEvent(el, 'click', function(e){
    // do stuff here
});

// later on, remove the event
// similar to clearTimeout()
reg.removeEvent(mem);

Personally, I don't use these direct-attachment functions very much because I prefer to use reglib's built-in event delegation, but it's nice to have them around.

More info: all reglib posts, reglib feed, download reglib

Saturday Nov 15, 2008

reglib versus JQuery

Read the reglib tag trail for more about reglib, or subscribe to the feed of all things tagged reglib.

Let me first of all stress that I'm not trying to bust on JQuery here. JQuery does something that needs to be done, and it does it just about as well as can be done given the tool-set browsers have collectively placed at our disposal.

With that said, I'm going to go ahead and pimp the reglib way of doing things, by which I mean declarative, over the load-traverse-modify methodology, which JQuery makes so easy.

The demo page linked below has two identical interactive widgets; one wired up using JQuery, and the other wired up using reglib.

» Demo Page «

The page is rigged like a science experiment, with a control followed by several tests in which you observe differences in behavior between the two widgets in response to various stimuli. The goal is to demonstrate reglib's resilience under duress (as it were). Enjoy.

Monday Nov 10, 2008

reglib JavaScript Library Now Available Under MIT License

The reglib is now published to Google Code and available under the MIT license.

Project Home: http://code.google.com/p/reglib/
Download: http://code.google.com/p/reglib/downloads/list

The backstory - Almost a year ago1 I made all kinds of big talk about releasing a JavaScript lib I was developing for sun.com. This library obviates (some of) the need for what I call the load-traverse-modify methodology of unobtrusive JavaScript:

  • Load: make it your business to know when the DOM has loaded.
  • Traverse: use a query to scan said DOM, returning a list of elements.
  • Modify: attach event handlers to, and otherwise manipulate, those elements.

Although LTM is quite common in JavaScript development, I believe it's an antipattern. The reglib tries to get closer to the CSS way of doing things: declare what kinds of elements get what behavior, and have that declaration take immediate effect at a global level, regardless of subsequent mutations to the DOM.

// JAVASCRIPT CODE:
reg.click("a.popup", function(e){
    window.open(this.href);
    return false;
});

/\*
ENGLISH TRANSLATION:
I declare, forthwith, that all clicks on <a> elements with
class "popup" shall be handled thusly...
\*/

So there you have it. Feel free to check it out (just check it out or actually check it out), read the documentation wiki, download, leave comments, etc. The lib also has a bunch of convenience functions for DOM manipulation, plus it has tools for doing LTM stuff because let's face it, sometimes there's no other way.

(Previous posts on this topic can be found here and here.)


1 It took so long because, in a nutshell: 1) Sun has an open source review process, you can't just release code willy-nilly, 2) During the process, I failed to be a squeaky wheel, and in fact the lawyers waited on my responses as much as I waited on their responses. However development over that interval has been constantly active, and reglib is being used on sun.com.

About

My name is Greg Reimer and I'm a web technologist for the Sun.COM web design team.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today