From e0aa10a664aabbc9ab900a07ea55cb892a7b8d3f Mon Sep 17 00:00:00 2001 From: Brandon Aaron Date: Sun, 22 Apr 2007 04:17:43 +0000 Subject: [PATCH] Fix scope issue in DOM 2 event handlers --- src/event/event.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/event/event.js b/src/event/event.js index 0d8204598..61bd4e885 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -12,7 +12,7 @@ jQuery.event = { // around, causing it to be cloned in the process if ( jQuery.browser.msie && element.setInterval != undefined ) element = window; - + // if data is passed, bind to handler if( data != undefined ) { // Create temporary function pointer to original handler @@ -22,13 +22,13 @@ jQuery.event = { handler = function() { // Pass arguments and context to original handler return fn.apply(this, arguments); - }; + }; // Store data in unique handler - handler.data = data; + handler.data = data; // Set the guid of unique handler to the same of original handler, so it can be removed - handler.guid = fn.guid; + handler.guid = fn.guid; } // Make sure that the function being executed has a unique ID @@ -48,12 +48,18 @@ jQuery.event = { // Add the function to the element's handler list handlers[handler.guid] = handler; + + if (!element.$handle) { + element.$handle = function() { + jQuery.event.handle.apply(element, arguments); + }; - // And bind the global event handler to the element - if (element.addEventListener) - element.addEventListener(type, this.handle, false); - else if (element.attachEvent) - element.attachEvent("on" + type, this.handle, false); + // And bind the global event handler to the element + if (element.addEventListener) + element.addEventListener(type, element.$handle, false); + else if (element.attachEvent) + element.attachEvent("on" + type, element.$handle, false); + } // Remember the function in a global list (for triggering) if (!this.global[type]) @@ -92,11 +98,11 @@ jQuery.event = { // remove generic event handler if no more handlers exist for ( ret in events[type] ) break; if ( !ret ) { - ret = null; if (element.removeEventListener) - element.removeEventListener(type, this.handle, false); + element.removeEventListener(type, element.$handle, false); else if (element.detachEvent) - element.detachEvent("on" + type, this.handle, false); + element.detachEvent("on" + type, element.$handle, false); + ret = element.$handle = null; delete events[type]; } } -- 2.39.5