diff --git a/CHANGELOG b/CHANGELOG index 8934083..2d4e595 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Make `Event.stopObserving` return element in all cases. [#810 state:resolved] (Yaffle, Tobie Langel) + * String#startsWith, String#endsWith performance optimization (Yaffle, Tobie Langel, kangax) * Rewrite String#camelize using String#replace with a replacement function (Phred, John-David Dalton, Samuel Lebeau, kangax) diff --git a/src/dom/event.js b/src/dom/event.js index 7980c52..0f35d65 100644 --- a/src/dom/event.js +++ b/src/dom/event.js @@ -617,44 +617,34 @@ function stopObserving(element, eventName, handler) { element = $(element); - var registry = Element.retrieve(element, 'prototype_event_registry'); + var registry = Element.retrieve(element, 'prototype_event_registry') + if (!registry) return element; - if (Object.isUndefined(registry)) return element; - - if (eventName && !handler) { - // If an event name is passed without a handler, we stop observing all - // handlers of that type. - var responders = registry.get(eventName); - - if (Object.isUndefined(responders)) return element; - - responders.each( function(r) { - Element.stopObserving(element, eventName, r.handler); - }); - return element; - } else if (!eventName) { - // If both the event name and the handler are omitted, we stop observing - // _all_ handlers on the element. + if (!eventName) { + // We stop observing all events. + // e.g.: $(element).stopObserving(); registry.each( function(pair) { - var eventName = pair.key, responders = pair.value; - - responders.each( function(r) { - Element.stopObserving(element, eventName, r.handler); - }); + var eventName = pair.key; + stopObserving(element, eventName); }); return element; } var responders = registry.get(eventName); + if (!responders) return element; - // Fail gracefully if there are no responders assigned. - if (!responders) return; + if (!handler) { + // We stop observing all handlers for the given eventName. + // e.g.: $(element).stopObserving('click'); + responders.each(function(r) { + stopObserving(element, eventName, r.handler); + }); + return element; + } var responder = responders.find( function(r) { return r.handler === handler; }); if (!responder) return element; - var actualEventName = _getDOMEventName(eventName); - if (eventName.include(':')) { // Custom event. if (element.removeEventListener) @@ -665,6 +655,7 @@ } } else { // Ordinary event. + var actualEventName = _getDOMEventName(eventName); if (element.removeEventListener) element.removeEventListener(actualEventName, responder, false); else diff --git a/test/unit/event_test.js b/test/unit/event_test.js index fc6df25..3e3416c 100644 --- a/test/unit/event_test.js +++ b/test/unit/event_test.js @@ -184,6 +184,8 @@ new Test.Unit.Runner({ span.observe("test:somethingHappened", observer); this.assertEqual(span, span.stopObserving("test:somethingHappened")); + this.assertEqual(span, span.stopObserving("test:somethingOtherHappened", observer)); + span.observe("test:somethingHappened", observer); this.assertEqual(span, span.stopObserving()); this.assertEqual(span, span.stopObserving()); // assert it again, after there are no observers