prototype: Make sure Element#stopObserving is always chainable.

This commit is contained in:
Sam Stephenson 2007-08-11 19:07:21 +00:00
parent 14ea77ef39
commit d2974cfca3
3 changed files with 20 additions and 5 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Make sure Element#stopObserving is always chainable. [sam]
* Event.fire/Element#fire now returns the newly-fired event instead of the event's target element. [sam]
* Restrict Event.DOMEvents to include only events that can be supported in all browsers. [sam]

View File

@ -186,18 +186,20 @@ Object.extend(Event, (function() {
var id = getEventID(element), name = getDOMEventName(eventName);
if (!handler && eventName) {
return getWrappersForEventName(id, eventName).each(function(wrapper) {
getWrappersForEventName(id, eventName).each(function(wrapper) {
element.stopObserving(eventName, wrapper.handler);
}) && false;
});
return element;
} else if (!eventName) {
return Object.keys(getCacheForID(id)).each(function(eventName) {
Object.keys(getCacheForID(id)).each(function(eventName) {
element.stopObserving(eventName);
}) && false;
});
return element;
}
var wrapper = findWrapper(id, eventName, handler);
if (!wrapper) return false;
if (!wrapper) return element;
if (element.removeEventListener) {
element.removeEventListener(name, wrapper, false);

View File

@ -183,6 +183,17 @@
assertEqual(0, Event.cache[eventID].somethingHappened.length);
}},
testStopObservingIsChainable: function() { with(this) {
var span = $("span"), observer = function() { };
span.observe("somethingHappened", observer);
assertEqual(span, span.stopObserving("somethingHappened", observer));
span.observe("somethingHappened", observer);
assertEqual(span, span.stopObserving("somethingHappened"));
span.observe("somethingHappened", observer);
assertEqual(span, span.stopObserving());
assertEqual(span, span.stopObserving()); // assert it again, after there are no observers
}},
testDocumentContentLoadedEventFiresBeforeWindowLoad: function() { with(this) {
assert(eventResults.contentLoaded, "contentLoaded");
assert(eventResults.contentLoaded.endOfDocument, "contentLoaded.endOfDocument");