Add a _leaflet_id to the map container to fix #4806 (#4810)

This commit is contained in:
Iván Sánchez Ortega 2016-08-11 09:10:46 +02:00 committed by Per Liedman
parent e1e121c746
commit 09937e39b0
2 changed files with 25 additions and 4 deletions

View File

@ -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 () {

View File

@ -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 () {