Live Events

Live Events


- The selector must be run, before the event handler is bound... this
seems less than ideal, if a large number of DOM elements already exist
- Always binds/listens to the document. My biggest hesitation with
using live for delegation, is that there is no way to control the
listener, it is always the document. It should at least use the jquery
collection's context.
- Functionality is private... would like to expose the internal
functions "liveConvert" and "liveHandler" to enable a plugin to
leverage some of the features.
- Can't attach data to handlers the way you could using "bind"
- Events don't bubble correctly. Using the "closest" method seems like
the wrong solution here... I love the method, it is quite useful, but
in this case it causes elements to need to be sorted, and stops the
event from bubbling the way events should
- Events and handlers are not cancelable... because of the way the
liveHandler works.
For the benefit of those who have not looked at the code...
"liveHandler" goes through all of the stored "live" handlers/
selectors, checks each handler for a matching event.type, then takes
the event.target and searches for the closest selector match. (This
means starting with the given element and walking up the parent tree
and testing the selector against every element until a match is found,
and storing the number of "parents" away the match is made) Then
matched elements and the current handler are stored in a temp array.
Afterwards, "liveHandler" sorts by "closest" then iterates the temp
array to finally call each handler. This all makes terrific sense, but
the biggest drawback for me, is that each handler is only called on
the first matched element.
If I have nested divs, I would expect the "live" handler for "click"
on selector "div" to get called twice. This is (currently) not the
case. To try to work out some of these issues, I have created a plugin
to try to fix most of these issues. Here is what I tried to
accomplish:
1) Exposes some of the private methods so that future plugins can
modify things a little more easily.
2) Bind the event listener to the jQuery context instead of the
document. (I think there must be a larger issue at play here) This
also only seems to work, by setting the context with a raw DOM element
like: $( context ).find( selector ) or $('selector',context).
3) "Better" bubbling/canceling... if a handler returns false, that
handler/selector stops. if event.stopImmediatePropagation() is called,
all handlers and selectors are stopped.
4) Creating a new method for setting up delegation without calling the
selector... using "liveConvert"
$( context ).delegate( type, selectot, handler );
Not sure yet about data... anyway, here is some code I have been
working on and some very basic tests...
http://dev.helgeson.info/live/
http://dev.helgeson.info/live/$.live.js
I know that there is probably some issue with "position" selectors,
but I just want to try to improve this powerful feature...