]> source.dussan.org Git - jquery.git/commitdiff
Make sure that unbinding on a plain javascript object works correctly. Fixes #6184.
authorjeresig <jeresig@gmail.com>
Fri, 26 Feb 2010 16:32:12 +0000 (11:32 -0500)
committerjeresig <jeresig@gmail.com>
Fri, 26 Feb 2010 16:32:12 +0000 (11:32 -0500)
src/event.js
test/unit/event.js

index 1e2d33dd61f607b991fce55df4dc6c687969267e..bfe2dd1af0896ae87eb2641f869373a19335f45c 100644 (file)
@@ -525,10 +525,14 @@ jQuery.event = {
 
 var removeEvent = document.removeEventListener ?
        function( elem, type, handle ) {
-               elem.removeEventListener( type, handle, false );
+               if ( elem.removeEventListener ) {
+                       elem.removeEventListener( type, handle, false );
+               }
        } : 
        function( elem, type, handle ) {
-               elem.detachEvent( "on" + type, handle );
+               if ( elem.detachEvent ) {
+                       elem.detachEvent( "on" + type, handle );
+               }
        };
 
 jQuery.Event = function( src ) {
index cbde90eee3aaf59c6718a64ed683cb4364ad8720..a220ebfc4db9adbc3d3da338d805c108797f2966 100644 (file)
@@ -373,6 +373,35 @@ test("bind(), with different this object", function() {
        ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
 });
 
+test("bind()/trigger()/unbind() on plain object", function() {
+       expect( 2 );
+
+       var obj = {};
+
+       // Make sure it doesn't complain when no events are found
+       jQuery(obj).trigger("test");
+
+       // Make sure it doesn't complain when no events are found
+       jQuery(obj).unbind("test");
+
+       jQuery(obj).bind("test", function(){
+               ok( true, "Custom event run." );
+       });
+
+       ok( jQuery(obj).data("events"), "Object has events bound." );
+
+       // Should trigger 1
+       jQuery(obj).trigger("test");
+
+       jQuery(obj).unbind("test");
+
+       // Should trigger 0
+       jQuery(obj).trigger("test");
+
+       // Make sure it doesn't complain when no events are found
+       jQuery(obj).unbind("test");
+});
+
 test("unbind(type)", function() {
        expect( 0 );