prototype: Event handlers are now bound to the observed element, not the event's target element.

This commit is contained in:
Sam Stephenson 2007-08-17 16:17:26 +00:00
parent aa1a7f5bf0
commit b2f884b4fa
3 changed files with 9 additions and 6 deletions

View File

@ -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ć]

View File

@ -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) {

View File

@ -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) {