Add Map#remove

This is a minimal implementation; it only unbinds events
and deletes the _leaflet flag from the container.

Refs #1101.
This commit is contained in:
John Firebaugh 2013-02-19 17:35:19 -08:00
parent 0b14d71d7a
commit 6ef25866eb
2 changed files with 34 additions and 4 deletions

View File

@ -6,6 +6,28 @@ describe("Map", function () {
spy = jasmine.createSpy(); spy = jasmine.createSpy();
}); });
describe("#remove", function () {
it("undefines container._leaflet", function () {
var container = document.createElement('div'),
map = new L.Map(container);
map.remove();
expect(container._leaflet).toBeUndefined();
});
it("unbinds events", function () {
var container = document.createElement('div'),
map = new L.Map(container).setView([0, 0], 1);
map.on('click dblclick mousedown mouseup mousemove', spy);
map.remove();
happen.click(container);
happen.dblclick(container);
happen.mousedown(container);
happen.mouseup(container);
happen.mousemove(container);
expect(spy).not.toHaveBeenCalled();
});
});
describe('#getCenter', function () { describe('#getCenter', function () {
it ('throws if not set before', function () { it ('throws if not set before', function () {
expect(function () { expect(function () {

View File

@ -239,6 +239,12 @@ L.Map = L.Class.extend({
return this; return this;
}, },
remove: function () {
this._initEvents('off');
delete this._container._leaflet;
return this;
},
// public methods for getting map state // public methods for getting map state
@ -571,21 +577,23 @@ L.Map = L.Class.extend({
// map events // map events
_initEvents: function () { _initEvents: function (onOff) {
if (!L.DomEvent) { return; } if (!L.DomEvent) { return; }
L.DomEvent.on(this._container, 'click', this._onMouseClick, this); onOff = onOff || 'on';
L.DomEvent[onOff](this._container, 'click', this._onMouseClick, this);
var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter', var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter',
'mouseleave', 'mousemove', 'contextmenu'], 'mouseleave', 'mousemove', 'contextmenu'],
i, len; i, len;
for (i = 0, len = events.length; i < len; i++) { for (i = 0, len = events.length; i < len; i++) {
L.DomEvent.on(this._container, events[i], this._fireMouseEvent, this); L.DomEvent[onOff](this._container, events[i], this._fireMouseEvent, this);
} }
if (this.options.trackResize) { if (this.options.trackResize) {
L.DomEvent.on(window, 'resize', this._onResize, this); L.DomEvent[onOff](window, 'resize', this._onResize, this);
} }
}, },