From 9d4d89445006c885c6f00b75b643ec7ac3b3c8ff Mon Sep 17 00:00:00 2001 From: danzel Date: Fri, 15 Mar 2013 13:53:03 +1300 Subject: [PATCH] Add another events test and fix from @iirvine --- spec/suites/core/EventsSpec.js | 21 +++++++++++++++++++++ src/core/Events.js | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spec/suites/core/EventsSpec.js b/spec/suites/core/EventsSpec.js index 817cdd10..440d2230 100644 --- a/spec/suites/core/EventsSpec.js +++ b/spec/suites/core/EventsSpec.js @@ -181,6 +181,27 @@ describe('Events', function() { expect(spy1).not.toHaveBeenCalled(); expect(spy2).not.toHaveBeenCalled(); }); + it('doesnt lose track of listeners when removing non existent ones', function () { + var obj = new Klass(), + spy = jasmine.createSpy(), + spy2 = jasmine.createSpy(), + foo = {}, + foo2 = {}; + + L.Util.stamp(foo); + L.Util.stamp(foo2); + + obj.addEventListener('test', spy, foo2); + + obj.removeEventListener('test', spy, foo); // Decrements test_idx to 0, even though event listener isn't registered with foo's _leaflet_id + obj.removeEventListener('test', spy, foo2); // Doesn't get removed + + obj.addEventListener('test', spy2, foo); + + obj.fireEvent('test'); + + expect(spy).not.toHaveBeenCalled(); + }); }); describe('#on, #off & #fire', function() { diff --git a/src/core/Events.js b/src/core/Events.js index 2a206bee..a6721a9b 100644 --- a/src/core/Events.js +++ b/src/core/Events.js @@ -99,7 +99,7 @@ L.Mixin.Events = { } } - if (contextId && listeners.length === 0 && events[objKey]) { + if (contextId && listeners.length === 0 && events[objKey] && events[objKey][contextId]) { objLenKey = objKey + '_len'; delete events[objKey][contextId]; events[objLenKey] = (events[objLenKey] || 1) - 1;