Ya, I guess it's more of performance issue? As I read from Simon
Willison's blog:
http://simonwillison.net/2008/Dec/23/live/

If then, say if I do this:

$$('div a').delegate('click', function(){
  alert('you clicked a link!');
});

.. then probably could delegate the event to the 'div' element instead
of all the way to document.body :P

On Dec 24, 2:08 pm, ibolmo <[email protected]> wrote:
> Not necessarily. Especially if you have a hierarchy of delegation. (oh
> wait.. but that's what DOM bubbling is all about).
>
> On Dec 23, 7:49 pm, cheeaun <[email protected]> wrote:
>
> > On Dec 24, 8:49 am, Aaron <[email protected]> wrote:
>
> > >...
>
> > > Secondly I have a new Element extension for event delegation. Event
> > > delegation is a common practice where by you attach an event listener
> > > to a parent object to monitor its children rather than attach events
> > > to all the children. It’s far more efficient when you have numerous
> > > items on a page that you want to interact with.
>
> > > Instead of doing
>
> > > $$('a').each(function(el) {
> > >    el.addEvent('click', function(){
> > >       alert('you clicked a link!');
> > >    });
>
> > > });
>
> > > which can have a big startup cost on a page full of links, you
> > > delegate the event to the parent:
>
> > > $(document.body).delegate('click', 'a', function(){
> > >    alert('you clicked a link!');
>
> > > });
>
> > > Only one event gets attached and it's far more efficient. Check out
> > > the longer article here:
>
> > >http://www.clientcide.com/code-releases/event-delegation-for-mootools/
>
> > > And examples in action here:
>
> > >http://www.clientcide.com/wiki/cnet-libraries/04-element/0.1-element....
>
> > This is cool but for some reason, I prefer the syntax to be something
> > like this?:
>
> > $(element).delegate('click', function(){
> >   alert('you clicked a link!');
>
> > });
>
> > .. though I think it's assumed that all events are added to
> > document.body
>
> > By the way, also watching this 
> > ticket:http://mootools.lighthouseapp.com/projects/2706/tickets/55-event-dele...
>
>

Reply via email to