diff --git a/spec/suites/core/EventsSpec.js b/spec/suites/core/EventsSpec.js index 99082673..8ef25ecc 100644 --- a/spec/suites/core/EventsSpec.js +++ b/spec/suites/core/EventsSpec.js @@ -126,6 +126,14 @@ 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(); + var removeNonExistentListener = function () { + obj.removeEventListener('test'); + }; + expect(removeNonExistentListener).to.not.throwException(); + }); + // 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..ac7d48a6 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(); }