From: jeresig Date: Sat, 13 Feb 2010 11:10:43 +0000 (-0500) Subject: Make sure that elements that have been removed also have their special events cleaned... X-Git-Tag: 1.4.2~6 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=da966573c3f3cb083c48cd3ead88e7542937d4df;p=jquery.git Make sure that elements that have been removed also have their special events cleaned up. Fixes #6084. --- diff --git a/src/event.js b/src/event.js index 6e65438ac..d91231e86 100644 --- a/src/event.js +++ b/src/event.js @@ -112,6 +112,10 @@ jQuery.event = { if ( special.add ) { special.add.call( elem, handleObj ); + + if ( !handleObj.handler.guid ) { + handleObj.handler.guid = handler.guid; + } } // Add the function to the element's handler list diff --git a/src/manipulation.js b/src/manipulation.js index a58e52b6a..4e7a31ed4 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -547,7 +547,7 @@ jQuery.extend({ }, cleanData: function( elems ) { - var data, id, cache = jQuery.cache; + var data, id, cache = jQuery.cache, special = jQuery.event.special; for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) { id = elem[ jQuery.expando ]; @@ -556,8 +556,13 @@ jQuery.extend({ data = cache[ id ]; if ( data.events ) { - for ( var event in data.events ) { - removeEvent( elem, event, data.handle ); + for ( var type in data.events ) { + if ( special[ type ] ) { + jQuery.event.remove( elem, type ); + + } else { + removeEvent( elem, type, data.handle ); + } } } diff --git a/test/unit/event.js b/test/unit/event.js index 21167c7de..7d66c9ca2 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -72,7 +72,7 @@ test("bind(), multiple events at once and namespaces", function() { }); test("bind(), namespace with special add", function() { - expect(19); + expect(24); var div = jQuery("
").bind("test", function(e) { ok( true, "Test event fired." ); @@ -97,7 +97,9 @@ test("bind(), namespace with special add", function() { handler.apply( this, arguments ); }; }, - remove: function() {} + remove: function() { + ok(true, "Remove called."); + } }; div.bind("test.a", {x: 1}, function(e) { @@ -119,7 +121,17 @@ test("bind(), namespace with special add", function() { // Should trigger 2 div.trigger("test.b"); + // Should trigger 4 div.unbind("test"); + + div = jQuery("
").bind("test", function(e) { + ok( true, "Test event fired." ); + }); + + // Should trigger 2 + div.appendTo("#main").remove(); + + delete jQuery.event.special.test; }); test("bind(), no data", function() {