]> source.dussan.org Git - jquery.git/commitdiff
Added in #690, the ability to remove an event handler from inside itself.
authorJohn Resig <jeresig@gmail.com>
Sat, 23 Dec 2006 17:41:00 +0000 (17:41 +0000)
committerJohn Resig <jeresig@gmail.com>
Sat, 23 Dec 2006 17:41:00 +0000 (17:41 +0000)
src/jquery/coreTest.js
src/jquery/jquery.js

index ada71959e34b79247164d110168a46fc34099ba5..67dea206f9e93a0277df708154f416a074e4a2ae 100644 (file)
@@ -564,3 +564,16 @@ test("removeClass(String) - add three classes and remove again", function() {
 test("removeAttr(String", function() {\r
        ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );\r
 });\r
+\r
+test("unbind(event)", function() {\r
+       expect(3);\r
+       var el = $("#firstp");\r
+       el.click(function() {\r
+               ok( true, "Fake normal bind" );\r
+       });\r
+       el.click(function(event) {\r
+               el.unbind(event);\r
+               ok( true, "Fake onebind" );\r
+       });\r
+       el.click().click();\r
+});\r
index d22d136f7eec4b0510bc7ba36c495ce49fdef9df..63b6ece9206ce1e834c205df02cd2a735f38d10a 100644 (file)
@@ -2090,7 +2090,9 @@ jQuery.extend({
                // Detach an event or set of events from an element
                remove: function(element, type, handler) {
                        if (element.events)
-                               if (type && element.events[type])
+                               if ( type && type.type )
+                                       delete element.events[ type.type ][ type.handler.guid ];
+                               else if (type && element.events[type])
                                        if ( handler )
                                                delete element.events[type][handler.guid];
                                        else
@@ -2135,6 +2137,10 @@ jQuery.extend({
                        args.unshift( event );
 
                        for ( var j in c ) {
+                               // Pass in a reference to the handler function itself
+                               // So that we can later remove it
+                               args[0].handler = c[j];
+
                                if ( c[j].apply( this, args ) === false ) {
                                        event.preventDefault();
                                        event.stopPropagation();