Fire layerremove after removing the layer

This is consistent with 0.5.x and seems more useful. Updating
the state of a layer switcher using map.hasLayer in response
to layeradd and layerremove events is my use case.
This commit is contained in:
John Firebaugh 2013-07-08 16:52:47 -07:00
parent a1d7c1268d
commit 4839c44509
2 changed files with 26 additions and 1 deletions

View File

@ -184,6 +184,16 @@ describe("Map", function () {
expect(spy.called).not.to.be.ok(); expect(spy.called).not.to.be.ok();
}); });
it("adds the layer before firing layeradd", function (done) {
var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() };
map.on('layeradd', function() {
expect(map.hasLayer(layer)).to.be.ok();
done();
});
map.setView([0, 0], 0);
map.addLayer(layer);
});
describe("When the first layer is added to a map", function () { describe("When the first layer is added to a map", function () {
it("fires a zoomlevelschange event", function () { it("fires a zoomlevelschange event", function () {
var spy = sinon.spy(); var spy = sinon.spy();
@ -270,6 +280,17 @@ describe("Map", function () {
expect(spy.called).not.to.be.ok(); expect(spy.called).not.to.be.ok();
}); });
it("removes the layer before firing layerremove", function (done) {
var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() };
map.on('layerremove', function() {
expect(map.hasLayer(layer)).not.to.be.ok();
done();
});
map.setView([0, 0], 0);
map.addLayer(layer);
map.removeLayer(layer);
});
describe("when the last tile layer on a map is removed", function () { describe("when the last tile layer on a map is removed", function () {
it("fires a zoomlevelschange event", function () { it("fires a zoomlevelschange event", function () {
map.whenReady(function(){ map.whenReady(function(){

View File

@ -209,10 +209,14 @@ L.Map = L.Class.extend({
if (this._loaded) { if (this._loaded) {
layer.onRemove(this); layer.onRemove(this);
this.fire('layerremove', {layer: layer});
} }
delete this._layers[id]; delete this._layers[id];
if (this._loaded) {
this.fire('layerremove', {layer: layer});
}
if (this._zoomBoundLayers[id]) { if (this._zoomBoundLayers[id]) {
delete this._zoomBoundLayers[id]; delete this._zoomBoundLayers[id];
this._updateZoomLevels(); this._updateZoomLevels();