Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add and update several features in FeatureLayer #1083

Merged
merged 4 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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([{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i noticed that the syntax errors i fixed in 872de35 didn't actually induce an error in your test because you're currently only introspecting the mock service response (and not the actual request parameters) which this lib actually has control over.

'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