Skip to content

Commit

Permalink
make sure addfeature is only called once
Browse files Browse the repository at this point in the history
  • Loading branch information
jgravois committed Nov 23, 2016
1 parent 9ad267e commit 2217ff8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
24 changes: 22 additions & 2 deletions spec/Layers/FeatureLayer/FeatureLayerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,30 @@ describe('L.esri.FeatureLayer', function () {
});

it('should add features back to a map', function () {
layer.removeLayers([1]);
layer = L.esri.featureLayer({
url: 'http://gis.example.com/mock/arcgis/rest/services/MockService/MockFeatureServer/0',
timeField: 'time',
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, {
color: 'green'
});
}
}).addTo(map);

var addSpy = sinon.spy();
layer.on('addfeature', addSpy);

var removeSpy = sinon.spy();
layer.on('removefeature', removeSpy);

layer.createLayers(features);
layer.removeLayers([1, 2]);
layer.addLayers([1]);

expect(map.hasLayer(layer.getFeature(1))).to.equal(true);
expect(map.hasLayer(layer.getFeature(2))).to.equal(true);
expect(map.hasLayer(layer.getFeature(2))).to.equal(false);
expect(addSpy.callCount).to.equal(3);
expect(removeSpy.callCount).to.equal(2);
});

it('should fire a addfeature event', function () {
Expand Down
13 changes: 9 additions & 4 deletions src/Layers/FeatureLayer/FeatureLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export var FeatureLayer = FeatureManager.extend({
this.fire('addfeature', {
feature: layer.feature
}, true);
layer.feature._alreadyAdded = true;
}

// update geometry if necessary
Expand Down Expand Up @@ -120,10 +121,11 @@ export var FeatureLayer = FeatureManager.extend({

// add the layer if the current zoom level is inside the range defined for the layer, it is within the current time bounds or our layer is not time enabled
if (this._visibleZoom() && (!this.options.timeField || (this.options.timeField && this._featureWithinTimeRange(geojson)))) {
this._map.addLayer(newLayer);
this.fire('addfeature', {
feature: newLayer.feature
}, true);
this._map.addLayer(newLayer);
newLayer.feature._alreadyAdded = true;
}
}
}
Expand All @@ -133,10 +135,12 @@ export var FeatureLayer = FeatureManager.extend({
for (var i = ids.length - 1; i >= 0; i--) {
var layer = this._layers[ids[i]];
if (layer) {
this.fire('addfeature', {
feature: layer.feature
}, true);
this._map.addLayer(layer);
if (!layer.feature._alreadyAdded) {
this.fire('addfeature', {
feature: layer.feature
}, true);
}
}
}
},
Expand All @@ -151,6 +155,7 @@ export var FeatureLayer = FeatureManager.extend({
permanent: permanent
}, true);
this._map.removeLayer(layer);
layer.feature._alreadyAdded = false;
}
if (layer && permanent) {
delete this._layers[id];
Expand Down

0 comments on commit 2217ff8

Please sign in to comment.