DON'T use it on $$ - that means you're delegating to all the anchors in
divs. Do:
$('myDiv').delegate('click', 'a', fn);

As for this suggestion:

>$(element).delegate('click', function(){
>  alert('you clicked a link!');
>});
>.. though I think it's assumed that all events are added to document.body

That couldn't work. What's element in this code? Delegation is all about
watching event bubbling for targets that match some test.

On Tue, Dec 23, 2008 at 10:46 PM, cheeaun (via Nabble) <
[email protected]<ml-user%[email protected]>
> wrote:

>
> 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 
> <olmo.maldon...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1714471&i=0>>
> 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 
> > <chee...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1714471&i=1>>
> wrote:
> >
> > > On Dec 24, 8:49 am, Aaron 
> > > <anut...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1714471&i=2>>
> 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...
> >
> >
>
>
> ------------------------------
>  View message @
> http://n2.nabble.com/New-Clientcide-plugins%3A-pointy-things%2C-event-delegation-and-more-tp1696536p1714471.html
> To start a new topic under MooTools Users, email
> [email protected]<ml-node%[email protected]>
> To unsubscribe from MooTools Users, click here< (link removed) >.
>
>
>


-----
The MooTools Tutorial:  http://www.mootorial.com www.mootorial.com 
Clientcide:  http://www.clientcide.com www.clientcide.com 
-- 
View this message in context: 
http://n2.nabble.com/New-Clientcide-plugins%3A-pointy-things%2C-event-delegation-and-more-tp1696536p1716230.html
Sent from the MooTools Users mailing list archive at Nabble.com.

Reply via email to