From 6ef25866ebffae84c73f191631606be75921d98a Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 19 Feb 2013 17:35:19 -0800 Subject: [PATCH] Add Map#remove This is a minimal implementation; it only unbinds events and deletes the _leaflet flag from the container. Refs #1101. --- spec/suites/map/MapSpec.js | 22 ++++++++++++++++++++++ src/map/Map.js | 16 ++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index dcaad7ef..9bc8a00b 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -6,6 +6,28 @@ describe("Map", function () { 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 () { it ('throws if not set before', function () { expect(function () { diff --git a/src/map/Map.js b/src/map/Map.js index f1f41647..9861f97d 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -239,6 +239,12 @@ L.Map = L.Class.extend({ return this; }, + remove: function () { + this._initEvents('off'); + delete this._container._leaflet; + return this; + }, + // public methods for getting map state @@ -571,21 +577,23 @@ L.Map = L.Class.extend({ // map events - _initEvents: function () { + _initEvents: function (onOff) { 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', 'mouseleave', 'mousemove', 'contextmenu'], i, len; 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) { - L.DomEvent.on(window, 'resize', this._onResize, this); + L.DomEvent[onOff](window, 'resize', this._onResize, this); } },