Skip to content

Commit

Permalink
Merge pull request Esri#1083 from Biboba/issue1055
Browse files Browse the repository at this point in the history
add and update several features in FeatureLayer
  • Loading branch information
jgravois committed Apr 30, 2018
2 parents 936575f + 9a8e130 commit d1f539e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 44 deletions.
48 changes: 48 additions & 0 deletions spec/Layers/FeatureLayer/FeatureManagerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,54 @@ describe('L.esri.FeatureManager', function () {
server.respond();
});

it('should wrap the updateFeatures method on the underlying service and refresh', function (done) {
server.respondWith('POST', 'http://gis.example.com/mock/arcgis/rest/services/MockService/MockFeatureServer/0/updateFeatures', JSON.stringify({
'updateResults': [{
'objectid': 1,
'success': true
}, {
'objectid': 2,
'success': true
}]
}));

layer.updateFeatures({
type: 'FeatureCollection',
features: [{
type: 'Feature',
id: 1,
properties: {
foo: 'bar'
},
geometry: {
type: 'Point',
coordinates: [-121, 45]
}
}, {
type: 'Feature',
id: 2,
properties: {
foo: 'bar'
},
geometry: {
type: 'Point',
coordinates: [-121, 45]
}
}]
}, function (error, response) {
expect(response).to.deep.equal([{
'objectid': 1,
'success': true
}, {
'objectid': 2,
'success': true
}]);
done();
});

server.respond();
});

it('should wrap the removeFeature method on the underlying service', function (done) {
server.respondWith('POST', 'http://gis.example.com/mock/arcgis/rest/services/MockService/MockFeatureServer/0/deleteFeatures', JSON.stringify({
'deleteResults': [{
Expand Down
2 changes: 0 additions & 2 deletions spec/Tasks/FindSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ describe('L.esri.Find', function () {
done();
});

console.log(request.url);

expect(request).to.be.an.instanceof(XMLHttpRequest);

server.respond();
Expand Down
2 changes: 0 additions & 2 deletions spec/Tasks/IdentifyImageSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,6 @@ describe('L.esri.IdentifyImage', function () {

task.returnGeometry(true).returnCatalogItems(true);
task.run(function (error, results, raw) {
console.log(JSON.stringify(results));
console.log(JSON.stringify(sampleResultsWithCatalogItems));
expect(results).to.deep.equal(sampleResultsWithCatalogItems);
expect(raw).to.deep.equal(sampleResponseWithCatalogItems);
done();
Expand Down
1 change: 0 additions & 1 deletion spec/Tasks/QuerySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,6 @@ describe('L.esri.Query', function () {
done();
});

console.log(request);
expect(request.requestBody).to.contain('foo=bar');
server.respond();
});
Expand Down
55 changes: 34 additions & 21 deletions src/Layers/FeatureLayer/FeatureManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export var FeatureManager = VirtualGrid.extend({
this.service.options.isModern = true;
}

if (metadata.objectIdField) {
this.service.options.idAttribute = metadata.objectIdField;
}

// add copyright text listed in service metadata
if (!this.options.attribution && map.attributionControl && metadata.copyrightText) {
this.options.attribution = metadata.copyrightText;
Expand Down Expand Up @@ -484,20 +488,27 @@ export var FeatureManager = VirtualGrid.extend({
},

addFeature: function (feature, callback, context) {
this.addFeatures(feature, callback, context);
},

addFeatures: function (features, callback, context) {
this._getMetadata(Util.bind(function (error, metadata) {
if (error) {
if (callback) { callback.call(this, error, null); }
return;
}
// GeoJSON featureCollection or simple feature
var featuresArray = features.features ? features.features : [features];

this.service.addFeature(feature, Util.bind(function (error, response) {
this.service.addFeatures(features, Util.bind(function (error, response) {
if (!error) {
// assign ID from result to appropriate objectid field from service metadata
feature.properties[metadata.objectIdField] = response.objectId;

// we also need to update the geojson id for createLayers() to function
feature.id = response.objectId;
this.createLayers([feature]);
for (var i = featuresArray.length - 1; i >= 0; i--) {
// assign ID from result to appropriate objectid field from service metadata
featuresArray[i].properties[metadata.objectIdField] = featuresArray.length > 1 ? response[i].objectId : response.objectId;
// we also need to update the geojson id for createLayers() to function
featuresArray[i].id = featuresArray.length > 1 ? response[i].objectId : response.objectId;
}
this.createLayers(featuresArray);
}

if (callback) {
Expand All @@ -508,10 +519,18 @@ export var FeatureManager = VirtualGrid.extend({
},

updateFeature: function (feature, callback, context) {
this.service.updateFeature(feature, function (error, response) {
this.updateFeatures(feature, callback, context);
},

updateFeatures: function (features, callback, context) {
// GeoJSON featureCollection or simple feature
var featuresArray = features.features ? features.features : [features];
this.service.updateFeatures(features, function (error, response) {
if (!error) {
this.removeLayers([feature.id], true);
this.createLayers([feature]);
for (var i = featuresArray.length - 1; i >= 0; i--) {
this.removeLayers([featuresArray[i].id], true);
}
this.createLayers(featuresArray);
}

if (callback) {
Expand All @@ -521,21 +540,15 @@ export var FeatureManager = VirtualGrid.extend({
},

deleteFeature: function (id, callback, context) {
this.service.deleteFeature(id, function (error, response) {
if (!error && response.objectId) {
this.removeLayers([response.objectId], true);
}
if (callback) {
callback.call(context, error, response);
}
}, this);
this.deleteFeatures(id, callback, context);
},

deleteFeatures: function (ids, callback, context) {
return this.service.deleteFeatures(ids, function (error, response) {
if (!error && response.length > 0) {
for (var i = 0; i < response.length; i++) {
this.removeLayers([response[i].objectId], true);
var responseArray = response.length ? response : [response];
if (!error && responseArray.length > 0) {
for (var i = responseArray.length - 1; i >= 0; i--) {
this.removeLayers([responseArray[i].objectId], true);
}
}
if (callback) {
Expand Down
46 changes: 28 additions & 18 deletions src/Services/FeatureLayerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,60 @@ export var FeatureLayerService = Service.extend({
},

addFeature: function (feature, callback, context) {
delete feature.id;

feature = geojsonToArcGIS(feature);
this.addFeatures(feature, callback, context);
},

addFeatures: function (features, callback, context) {
var featuresArray = features.features ? features.features : [features];
for (var i = featuresArray.length - 1; i >= 0; i--) {
delete featuresArray[i].id;
}
features = geojsonToArcGIS(features);
features = featuresArray.length > 1 ? features : [features];
return this.post('addFeatures', {
features: [feature]
features: features
}, function (error, response) {
var result = (response && response.addResults) ? response.addResults[0] : undefined;
// For compatibility reason with former addFeature function,
// we return the object in the array and not the array itself
var result = (response && response.addResults) ? response.addResults.length > 1 ? response.addResults : response.addResults[0] : undefined;
if (callback) {
callback.call(context, error || response.addResults[0].error, result);
}
}, context);
},

updateFeature: function (feature, callback, context) {
feature = geojsonToArcGIS(feature, this.options.idAttribute);
this.updateFeatures(feature, callback, context);
},

updateFeatures: function (features, callback, context) {
var featuresArray = features.features ? features.features : [features];
features = geojsonToArcGIS(features, this.options.idAttribute);
features = featuresArray.length > 1 ? features : [features];

return this.post('updateFeatures', {
features: [feature]
features: features
}, function (error, response) {
var result = (response && response.updateResults) ? response.updateResults[0] : undefined;
// For compatibility reason with former updateFeature function,
// we return the object in the array and not the array itself
var result = (response && response.updateResults) ? response.updateResults.length > 1 ? response.updateResults : response.updateResults[0] : undefined;
if (callback) {
callback.call(context, error || response.updateResults[0].error, result);
}
}, context);
},

deleteFeature: function (id, callback, context) {
return this.post('deleteFeatures', {
objectIds: id
}, function (error, response) {
var result = (response && response.deleteResults) ? response.deleteResults[0] : undefined;
if (callback) {
callback.call(context, error || response.deleteResults[0].error, result);
}
}, context);
this.deleteFeatures(id, callback, context);
},

deleteFeatures: function (ids, callback, context) {
return this.post('deleteFeatures', {
objectIds: ids
}, function (error, response) {
// pass back the entire array
var result = (response && response.deleteResults) ? response.deleteResults : undefined;
// For compatibility reason with former deleteFeature function,
// we return the object in the array and not the array itself
var result = (response && response.deleteResults) ? response.deleteResults.length > 1 ? response.deleteResults : response.deleteResults[0] : undefined;
if (callback) {
callback.call(context, error || response.deleteResults[0].error, result);
}
Expand Down

0 comments on commit d1f539e

Please sign in to comment.