From 11f25aa99511de8e99c1534265de0cca3740379a Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sat, 11 Aug 2007 19:53:39 +0000 Subject: [PATCH] prototype: Make sure Element#observe is always chainable. --- CHANGELOG | 2 +- src/event.js | 2 +- test/unit/event.html | 11 +++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2d0ef6b..12904e1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ *SVN* -* Make sure Element#stopObserving is always chainable. [sam] +* Make sure Element#observe and Element#stopObserving are always chainable. [sam] * Event.fire/Element#fire now returns the newly-fired event instead of the event's target element. [sam] diff --git a/src/event.js b/src/event.js index 6a6d68b..3cac071 100644 --- a/src/event.js +++ b/src/event.js @@ -170,7 +170,7 @@ Object.extend(Event, (function() { var id = getEventID(element), name = getDOMEventName(eventName); var wrapper = createWrapper(id, eventName, handler); - if (!wrapper) return false; + if (!wrapper) return element; if (element.addEventListener) { element.addEventListener(name, wrapper, false); diff --git a/test/unit/event.html b/test/unit/event.html index 6c2f365..df761a4 100644 --- a/test/unit/event.html +++ b/test/unit/event.html @@ -183,15 +183,22 @@ assertEqual(0, Event.cache[eventID].somethingHappened.length); }}, - testStopObservingIsChainable: function() { with(this) { + testObserveAndStopObservingAreChainable: function() { with(this) { var span = $("span"), observer = function() { }; - span.observe("somethingHappened", observer); + + assertEqual(span, 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 + + span.observe("somethingHappened", observer); + assertEqual(span, span.observe("somethingHappened", observer)); // try to reuse the same observer + span.stopObserving(); }}, testDocumentContentLoadedEventFiresBeforeWindowLoad: function() { with(this) {