Permalink
Browse files

Fix for #964

  • Loading branch information...
1 parent b15ae79 commit 34cb5b5812db50d921f7c7a0c398eb68167b3da3 @brandonaaron brandonaaron committed Feb 25, 2007
Showing with 30 additions and 7 deletions.
  1. +1 −0 ChangeLog.txt
  2. +24 −6 src/event/event.js
  3. +5 −1 src/event/eventTest.js
View
@@ -4,6 +4,7 @@
=== 1.1.2 ===
+* Event handlers (like element.onclick) are now removed when no more functions are bound to the event.
* Fixed DOM Manipulations for form elements.
* Fixed jQuery.isFunction to return false on nodes.
* Fixed jQuery.className.has, escaping regex characters in className (for metadata)
View
@@ -55,18 +55,36 @@ jQuery.event = {
// Detach an event or set of events from an element
remove: function(element, type, handler) {
- if (element.$events)
- if ( type && type.type )
- delete element.$events[ type.type ][ type.handler.guid ];
- else if (type && element.$events[type])
+ if (element.$events) {
+ var i,j,k;
+ if ( type && type.type ) { // type is actually an event object here
+ handler = type.handler;
+ type = type.type;
+ }
+
+ if (type && element.$events[type])
+ // remove the given handler for the given type
if ( handler )
delete element.$events[type][handler.guid];
+
+ // remove all handlers for the given type
else
- for ( var i in element.$events[type] )
+ for ( i in element.$events[type] )
delete element.$events[type][i];
+
+ // remove all handlers
else
- for ( var j in element.$events )
+ for ( j in element.$events )
this.remove( element, j );
+
+ // remove event handler if no more handlers exist
+ for ( k in element.$events[type] )
+ if (k) {
+ k = true;
+ break;
+ }
+ if (!k) element["on" + type] = null;
+ }
},
trigger: function(type, data, element) {
@@ -15,7 +15,7 @@ test("toggle(Function, Function) - add toggle event and fake a few clicks", func
});
test("unbind(event)", function() {
- expect(3);
+ expect(4);
var el = $("#firstp");
el.click(function() {
ok( true, "Fake normal bind" );
@@ -25,6 +25,10 @@ test("unbind(event)", function() {
ok( true, "Fake onebind" );
});
el.click().click();
+
+ el.click(function() { return; });
+ el.unbind('click');
+ ok( !el[0].onclick, "Handler is removed" ); // Bug #964
});
test("trigger(event, [data]", function() {

0 comments on commit 34cb5b5

Please sign in to comment.