From 7f5179b65431b77e89fa32623a5000e3630c191c Mon Sep 17 00:00:00 2001 From: jeresig Date: Fri, 26 Feb 2010 11:32:12 -0500 Subject: [PATCH] Make sure that unbinding on a plain javascript object works correctly. Fixes #6184. --- src/event.js | 8 ++++++-- test/unit/event.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/event.js b/src/event.js index 1e2d33dd6..bfe2dd1af 100644 --- a/src/event.js +++ b/src/event.js @@ -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 ) { diff --git a/test/unit/event.js b/test/unit/event.js index cbde90eee..a220ebfc4 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -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 ); -- 2.39.5