diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index ef8bb641..31d25ac0 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -69,6 +69,19 @@ describe("Map", function () { expect(spy.called).to.not.be.ok(); }); + + it("throws error if container is reused by other instance", function () { + var container = document.createElement('div'), + map = L.map(container), + map2; + + map.remove(); + map2 = L.map(container); + + expect(function () { + map.remove(); + }).to.throwException(); + }); }); describe('#getCenter', function () { diff --git a/src/map/Map.js b/src/map/Map.js index 8188c00a..e2e459d8 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -405,11 +405,19 @@ L.Map = L.Evented.extend({ this._initEvents(true); + if (this._containerId !== this._container._leaflet_id) { + throw new Error('Map container is being reused by another instance'); + } + try { // throws error in IE6-8 - delete this._container._leaflet; + delete this._container._leaflet_id; + delete this._containerId; } catch (e) { - this._container._leaflet = undefined; + /*eslint-disable */ + this._container._leaflet_id = undefined; + /*eslint-enable */ + this._containerId = undefined; } L.DomUtil.remove(this._mapPane); @@ -712,12 +720,12 @@ L.Map = L.Evented.extend({ if (!container) { throw new Error('Map container not found.'); - } else if (container._leaflet) { + } else if (container._leaflet_id) { throw new Error('Map container is already initialized.'); } L.DomEvent.addListener(container, 'scroll', this._onScroll, this); - container._leaflet = true; + this._containerId = L.Util.stamp(container); }, _initLayout: function () {