Permalink
Browse files

Add .delegate() and .undelegate(). An alternative to using .live() wh…

…ich goes from a single root and filters by the specified selectors. Should be used like do: .delegate(td, hover, someFn);. Fixes #6005.
  • Loading branch information...
jeresig committed Feb 1, 2010
1 parent cb65daa commit 31432e048f879b93ffa44c39d6f5989ab2620bd8
Showing with 433 additions and 38 deletions.
  1. +22 −6 src/event.js
  2. +411 −32 test/unit/event.js
View
@@ -866,6 +866,20 @@ jQuery.fn.extend({
return this;
},
delegate: function( selector, types, data, fn ) {
return this.live( types, data, fn, selector );
},
undelegate: function( selector, types, fn ) {
if ( arguments.length === 0 ) {
return this.unbind( "live" );
} else {
return this.die( types, null, fn, selector );
}
},
trigger: function( type, data ) {
return this.each(function() {
jQuery.event.trigger( type, data, this );
@@ -910,8 +924,10 @@ jQuery.fn.extend({
});
jQuery.each(["live", "die"], function( i, name ) {
jQuery.fn[ name ] = function( types, data, fn ) {
var type, i = 0;
jQuery.fn[ name ] = function( types, data, fn, origSelector /* Internal Use Only */ ) {
var type, i = 0,
selector = origSelector || this.selector,
context = origSelector ? this : jQuery( this.context );
if ( jQuery.isFunction( data ) ) {
fn = data;
@@ -928,13 +944,13 @@ jQuery.each(["live", "die"], function( i, name ) {
if ( name === "live" ) {
// bind live handler
jQuery( this.context ).bind( liveConvert( type, this.selector ), {
data: data, selector: this.selector, live: type
context.bind( liveConvert( type, selector ), {
data: data, selector: selector, live: type
}, fn );
} else {
// unbind live handler
jQuery( this.context ).unbind( liveConvert( type, this.selector ), fn ? { guid: fn.guid + this.selector + type } : null );
context.unbind( liveConvert( type, selector ), fn ? { guid: fn.guid + selector + type } : null );
}
}
@@ -1002,7 +1018,7 @@ function liveHandler( event ) {
}
function liveConvert( type, selector ) {
return "live." + (type ? type + "." : "") + selector.replace(/\./g, "`").replace(/ /g, "&");
return "live." + (type && type !== "*" ? type + "." : "") + selector.replace(/\./g, "`").replace(/ /g, "&");
}
jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
Oops, something went wrong.

3 comments on commit 31432e0

@louisremi

This comment has been minimized.

Show comment
Hide comment
@louisremi

louisremi Feb 2, 2010

Contributor

We now have two ways to do event delegation in jQuery.
One uses the good old jQuery syntax we all know but seems somewhat magical before you know how event delegation works.
The other requires a good understanding of event delegation but seems much better from a performance point of view.

Is delegate already compatible with mouseenter/mouseleave, change, submit and focus/blur?

Contributor

louisremi replied Feb 2, 2010

We now have two ways to do event delegation in jQuery.
One uses the good old jQuery syntax we all know but seems somewhat magical before you know how event delegation works.
The other requires a good understanding of event delegation but seems much better from a performance point of view.

Is delegate already compatible with mouseenter/mouseleave, change, submit and focus/blur?

@jeresig

This comment has been minimized.

Show comment
Hide comment
@jeresig

jeresig Feb 2, 2010

Member

Yes. As you'll note, delegate just uses live - therefore it supports everything that live supports.

Member

jeresig replied Feb 2, 2010

Yes. As you'll note, delegate just uses live - therefore it supports everything that live supports.

@louisremi

This comment has been minimized.

Show comment
Hide comment
@louisremi

louisremi Feb 2, 2010

Contributor

Great!

Contributor

louisremi replied Feb 2, 2010

Great!

Please sign in to comment.