From d2974cfca3bcf410fc72b3e7b906aceb01fc2b21 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sat, 11 Aug 2007 19:07:21 +0000 Subject: [PATCH] prototype: Make sure Element#stopObserving is always chainable. --- CHANGELOG | 2 ++ src/event.js | 12 +++++++----- test/unit/event.html | 11 +++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 36d2b75..2d0ef6b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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] diff --git a/src/event.js b/src/event.js index 21ddd03..6a6d68b 100644 --- a/src/event.js +++ b/src/event.js @@ -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); diff --git a/test/unit/event.html b/test/unit/event.html index a8aba77..6c2f365 100644 --- a/test/unit/event.html +++ b/test/unit/event.html @@ -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");