From 36554c08742e909be1913b5d017b2f9292136913 Mon Sep 17 00:00:00 2001 From: john gravois Date: Wed, 23 Nov 2016 13:36:33 -0800 Subject: [PATCH 1/2] make sure addfeature is only called once --- spec/Layers/FeatureLayer/FeatureLayerSpec.js | 24 ++++++++++++++++++-- src/Layers/FeatureLayer/FeatureLayer.js | 13 +++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/spec/Layers/FeatureLayer/FeatureLayerSpec.js b/spec/Layers/FeatureLayer/FeatureLayerSpec.js index 0815f3046..5a4f0cb93 100644 --- a/spec/Layers/FeatureLayer/FeatureLayerSpec.js +++ b/spec/Layers/FeatureLayer/FeatureLayerSpec.js @@ -143,10 +143,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 () { diff --git a/src/Layers/FeatureLayer/FeatureLayer.js b/src/Layers/FeatureLayer/FeatureLayer.js index 84e6fe9f3..73069b700 100644 --- a/src/Layers/FeatureLayer/FeatureLayer.js +++ b/src/Layers/FeatureLayer/FeatureLayer.js @@ -90,6 +90,7 @@ export var FeatureLayer = FeatureManager.extend({ this.fire('addfeature', { feature: layer.feature }, true); + layer.feature._alreadyAdded = true; } // update geometry if necessary @@ -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; } } } @@ -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); + } } } }, @@ -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]; From 52d18262e990e6a5a9889f3212030b555861d814 Mon Sep 17 00:00:00 2001 From: john gravois Date: Fri, 30 Dec 2016 15:42:42 -0800 Subject: [PATCH 2/2] create OR add --- spec/Layers/FeatureLayer/FeatureLayerSpec.js | 6 +++--- src/Layers/FeatureLayer/FeatureLayer.js | 11 ----------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/spec/Layers/FeatureLayer/FeatureLayerSpec.js b/spec/Layers/FeatureLayer/FeatureLayerSpec.js index 5a4f0cb93..e41e23ac9 100644 --- a/spec/Layers/FeatureLayer/FeatureLayerSpec.js +++ b/spec/Layers/FeatureLayer/FeatureLayerSpec.js @@ -153,8 +153,8 @@ describe('L.esri.FeatureLayer', function () { } }).addTo(map); - var addSpy = sinon.spy(); - layer.on('addfeature', addSpy); + var createSpy = sinon.spy(); + layer.on('createfeature', createSpy); var removeSpy = sinon.spy(); layer.on('removefeature', removeSpy); @@ -165,7 +165,7 @@ describe('L.esri.FeatureLayer', function () { expect(map.hasLayer(layer.getFeature(1))).to.equal(true); expect(map.hasLayer(layer.getFeature(2))).to.equal(false); - expect(addSpy.callCount).to.equal(3); + expect(createSpy.callCount).to.equal(2); expect(removeSpy.callCount).to.equal(2); }); diff --git a/src/Layers/FeatureLayer/FeatureLayer.js b/src/Layers/FeatureLayer/FeatureLayer.js index 73069b700..e674838ce 100644 --- a/src/Layers/FeatureLayer/FeatureLayer.js +++ b/src/Layers/FeatureLayer/FeatureLayer.js @@ -90,7 +90,6 @@ export var FeatureLayer = FeatureManager.extend({ this.fire('addfeature', { feature: layer.feature }, true); - layer.feature._alreadyAdded = true; } // update geometry if necessary @@ -122,10 +121,6 @@ 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); - newLayer.feature._alreadyAdded = true; } } } @@ -136,11 +131,6 @@ export var FeatureLayer = FeatureManager.extend({ var layer = this._layers[ids[i]]; if (layer) { this._map.addLayer(layer); - if (!layer.feature._alreadyAdded) { - this.fire('addfeature', { - feature: layer.feature - }, true); - } } } }, @@ -155,7 +145,6 @@ export var FeatureLayer = FeatureManager.extend({ permanent: permanent }, true); this._map.removeLayer(layer); - layer.feature._alreadyAdded = false; } if (layer && permanent) { delete this._layers[id];