]> source.dussan.org Git - jquery.git/commitdiff
Make jQuery().off(event) work for delegated events.
authorDave Methvin <dave.methvin@gmail.com>
Mon, 24 Oct 2011 02:25:13 +0000 (22:25 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Mon, 24 Oct 2011 15:18:18 +0000 (11:18 -0400)
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.

src/event.js
test/unit/event.js

index 42d30e9aab38df434d8c52aaaeae14bc1e612dd0..64cfda5308affef11141676f47b535c0e18fbe4e 100644 (file)
@@ -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] )
index 905e6ab99b15ef1b3bb1e3e7b80b805e428d2697..bb622f57775264c596c5a31eb44d9bdf37ff8d8e 100644 (file)
@@ -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);