diff --git a/CHANGELOG b/CHANGELOG index 3806eed..2c64bc4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Event handlers are now bound to the observed element, not the event's target element. [Dan Webb] + * Define Node constants conditionally after checking for Node.ELEMENT_NODE presence. Add unit test to check the values of all constants. Closes #7625. [Mislav Marohnić] * Make sure Enumerable#include doesn't do type comparision. [Mislav Marohnić] diff --git a/src/event.js b/src/event.js index eba3da7..ad095e6 100644 --- a/src/event.js +++ b/src/event.js @@ -127,7 +127,8 @@ Object.extend(Event, (function() { return c[eventName] = c[eventName] || []; } - function createWrapper(id, eventName, handler) { + function createWrapper(element, eventName, handler) { + var id = getEventID(element); var c = getWrappersForEventName(id, eventName); if (c.pluck("handler").include(handler)) return false; @@ -136,7 +137,7 @@ Object.extend(Event, (function() { return false; Event.extend(event); - handler.call(event.target, event); + handler.call(element, event) }; wrapper.handler = handler; @@ -168,9 +169,9 @@ Object.extend(Event, (function() { return { observe: function(element, eventName, handler) { element = $(element); - var id = getEventID(element), name = getDOMEventName(eventName); + var name = getDOMEventName(eventName); - var wrapper = createWrapper(id, eventName, handler); + var wrapper = createWrapper(element, eventName, handler); if (!wrapper) return element; if (element.addEventListener) { diff --git a/test/unit/event.html b/test/unit/event.html index df761a4..1e7af77 100644 --- a/test/unit/event.html +++ b/test/unit/event.html @@ -113,7 +113,7 @@ span.stopObserving("somethingHappened", observer); }}, - testEventObserversAreBoundToTheTargetElement: function() { with(this) { + testEventObserversAreBoundToTheObservedElement: function() { with(this) { var span = $("span"), target, observer = function() { target = this }; span.observe("somethingHappened", observer); @@ -126,7 +126,7 @@ outer.observe("somethingHappened", observer); span.fire("somethingHappened"); outer.stopObserving("somethingHappened", observer); - assertEqual(span, target); + assertEqual(outer, target); }}, testMultipleCustomEventObserversWithTheSameHandler: function() { with(this) {