Skip to content
Permalink
Browse files

Make jQuery().off(event) work for delegated events.

Logic to handle detaching by event was in both .off() and jQuery.event.remove; now it's only in .off(). It's a bit of a strange case since the event object (not the jQuery set) specifies the element.
  • Loading branch information...
dmethvin committed Oct 24, 2011
1 parent b208042 commit 4ac6f8d9d322c7e468ed0d58e402abe7bdca354d
Showing with 25 additions and 11 deletions.
  1. +11 −11 src/event.js
  2. +14 −0 test/unit/event.js
@@ -166,13 +166,6 @@ jQuery.event = {
return;
}

// For removal, types can be an Event object
if ( types && types.type && types.handler ) {
handler = types.handler;
types = types.type;
selector = types.selector;
}

// Once for each type.namespace in types; type may be omitted
types = (types || "").replace( rhoverHack, "mouseover$1 mouseout$1" ).split(" ");
for ( t = 0; t < types.length; t++ ) {
@@ -919,7 +912,8 @@ jQuery.fn.extend({
if ( one === 1 ) {
origFn = fn;
fn = function( event ) {
jQuery.event.remove( event.currentTarget || this, event );
// Can use an empty set, since event contains the info
jQuery().off( event );
return origFn.apply( this, arguments );
};
// Use same guid so caller can remove using origFn
@@ -933,9 +927,15 @@ jQuery.fn.extend({
return this.on.call( this, types, selector, data, fn, 1 );
},
off: function( types, selector, fn ) {
if ( types && types.preventDefault ) {
// ( event ) native or jQuery.Event
return this.off( types.type, types.handler, types.selector );
if ( types && types.preventDefault && types.handleObj ) {
// ( event ) dispatched jQuery.Event
var handleObj = types.handleObj;
jQuery( types.currentTarget ).off(
handleObj.namespace? handleObj.type + "." + handleObj.namespace : handleObj.type,
handleObj.selector,
handleObj.handler
);
return this;
}
if ( typeof types === "object" ) {
// ( types-object [, selector] )
@@ -2054,6 +2054,20 @@ test(".delegate()/.undelegate()", function() {
jQuery("#body").undelegate("#nothiddendiv div", "click");
});

test("jQuery.off using dispatched jQuery.Event", function() {
expect(1);

var markup = jQuery( '<p><a href="#">target</a></p>' ),
count = 0;
markup
.on( "click.name", "a", function( event ) {
equals( ++count, 1, "event called once before removal" );
jQuery().off( event );
})
.find( "a" ).click().click().end()
.remove();
});

test("stopPropagation() stops directly-bound events on delegated target", function() {
expect(1);

0 comments on commit 4ac6f8d

Please sign in to comment.
You can’t perform that action at this time.