Fire an unload event on removal

Plugins that need to bind events to `window` or `document`
can attach a listener for this event and unbind their event
handlers.

However, for symmetry, unload is fired only if load has been
fired.
This commit is contained in:
John Firebaugh 2013-02-21 17:29:41 -08:00
parent 2aae4a0556
commit 46dddc895c
2 changed files with 19 additions and 0 deletions

View File

@ -7,6 +7,22 @@ describe("Map", function () {
});
describe("#remove", function () {
it("fires an unload event if loaded", function () {
var container = document.createElement('div'),
map = new L.Map(container).setView([0, 0], 0);
map.on('unload', spy);
map.remove();
expect(spy).toHaveBeenCalled();
});
it("fires no unload event if not loaded", function () {
var container = document.createElement('div'),
map = new L.Map(container);
map.on('unload', spy);
map.remove();
expect(spy).not.toHaveBeenCalled();
});
it("undefines container._leaflet", function () {
var container = document.createElement('div'),
map = new L.Map(container);

View File

@ -240,6 +240,9 @@ L.Map = L.Class.extend({
},
remove: function () {
if (this._loaded) {
this.fire('unload');
}
this._initEvents('off');
delete this._container._leaflet;
return this;