From 52498f1ef9493e2c4d2a8dabc07d57fec7649e46 Mon Sep 17 00:00:00 2001 From: Daan Mortier Date: Mon, 29 Apr 2013 11:53:16 +0200 Subject: [PATCH] Issue #1631: Fixed a bug on removeEventListener Bugfix for issue #1631 where removeEventListener would throw when no event listeners are registered on the object. --- spec/suites/core/EventsSpec.js | 5 +++++ src/core/Events.js | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/spec/suites/core/EventsSpec.js b/spec/suites/core/EventsSpec.js index 99082673..0ee0d1a8 100644 --- a/spec/suites/core/EventsSpec.js +++ b/spec/suites/core/EventsSpec.js @@ -126,6 +126,11 @@ describe('Events', function() { expect(spy5.called).to.be(false); }); + it('can handle calls to #removeEventListener on objects with no registered event listeners', function () { + var obj = new Klass(); + obj.removeEventListener('test'); + }); + // added due to context-sensitive removeListener optimization it('fires multiple listeners with the same context with id', function () { var obj = new Klass(), diff --git a/src/core/Events.js b/src/core/Events.js index 71da700b..4e54c532 100644 --- a/src/core/Events.js +++ b/src/core/Events.js @@ -63,6 +63,10 @@ L.Mixin.Events = { removeEventListener: function (types, fn, context) { // ([String, Function, Object]) or (Object[, Object]) + if (!this[eventsKey]) { + return this; + } + if (!types) { return this.clearAllEventListeners(); }