From f0913c0f24f0ce63f82abb043ea692d967091d7c Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Tue, 1 Nov 2016 09:37:10 -0600 Subject: [PATCH] Closes #491, #492 --- lib/edit/OfflineEditAdvanced.js | 36 ++++++++++++++++++++++++++++--- lib/edit/OfflineEditBasic.js | 38 ++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/lib/edit/OfflineEditAdvanced.js b/lib/edit/OfflineEditAdvanced.js index b9e5c11..d1458f5 100644 --- a/lib/edit/OfflineEditAdvanced.js +++ b/lib/edit/OfflineEditAdvanced.js @@ -25,7 +25,8 @@ define([ _featureLayers: {}, _featureCollectionUsageFlag: false, // if a feature collection was used to create the feature layer. _editStore: new O.esri.Edit.EditStore(), - _defaultXhrTimeout: 15000, // ms + _defaultXhrTimeout: 15000, // ms + _esriFieldTypeOID: "", // Determines the correct casing for objectid. Some feature layers use different casing ONLINE: "online", // all edits will directly go to the server OFFLINE: "offline", // edits will be enqueued @@ -121,6 +122,14 @@ define([ // library will break and we'll have to re-architect how it manages UIDs. layer.objectIdField = this.DB_UID; + // NOTE: set the casing for the feature layers objectid. + for(var i = 0; i < layer.fields.length; i++){ + if(layer.fields[i].type === "esriFieldTypeOID"){ + this._esriFieldTypeOID = layer.fields[i].name; + break; + } + } + var url = null; // There have been reproducible use cases showing when a browser is restarted offline that @@ -1896,7 +1905,28 @@ define([ }); } + // addResults present a special case for handling objectid if(addResults.length > 0) { + var objectid = ""; + + if(addResults[0].hasOwnProperty("objectid")){ + objectid = "objectid"; + } + + if(addResults[0].hasOwnProperty("objectId")){ + objectid = "objectId"; + } + + if(addResults[0].hasOwnProperty("OBJECTID")){ + objectid = "OBJECTID"; + } + + // ??? These are the most common objectid values. I may have missed some! + + // Some feature layers will return different casing such as: 'objectid', 'objectId' and 'OBJECTID' + // Normalize these values to the feature type OID so that we don't break other aspects + // of the JS API. + adds[0].attributes[that._esriFieldTypeOID] = addResults[0][objectid]; var graphic = new Graphic(adds[0].geometry,null,adds[0].attributes); layer.add(graphic); } @@ -2051,11 +2081,11 @@ define([ if( req.status === 200 && req.responseText !== "") { try { - var obj = JSON.parse(this.response); + var obj = JSON.parse(this.responseText); callback(obj.addResults, obj.updateResults, obj.deleteResults); } catch(err) { - console.error("EDIT REQUEST RESPONSE WAS NOT SUCCESSFUL:", req); + console.error("FAILED TO PARSE EDIT REQUEST RESPONSE:", req); errback("Unable to parse xhr response", req); } } diff --git a/lib/edit/OfflineEditBasic.js b/lib/edit/OfflineEditBasic.js index af20c0e..c13c30a 100644 --- a/lib/edit/OfflineEditBasic.js +++ b/lib/edit/OfflineEditBasic.js @@ -24,8 +24,9 @@ define([ _onlineStatus: "online", _featureLayers: {}, _editStore: new O.esri.Edit.EditStorePOLS(), - _defaultXhrTimeout: 15000, // ms + _defaultXhrTimeout: 15000, // ms _autoOfflineDetect: true, + _esriFieldTypeOID: "", // Determines the correct casing for objectid. Some feature layers use different casing ONLINE: "online", // all edits will directly go to the server OFFLINE: "offline", // edits will be enqueued @@ -76,6 +77,14 @@ define([ // library will break and we'll have to re-architect how it manages UIDs. layer.objectIdField = this.DB_UID; + // NOTE: set the casing for the feature layers objectid. + for(var i = 0; i < layer.fields.length; i++){ + if(layer.fields[i].type === "esriFieldTypeOID"){ + this._esriFieldTypeOID = layer.fields[i].name; + break; + } + } + var url = null; // There have been reproducible use cases showing when a browser is restarted offline that @@ -820,7 +829,29 @@ define([ this._makeEditRequest(layer, adds, updates, deletes, function (addResults, updateResults, deleteResults) { + // addResults present a special case for handling objectid if(addResults.length > 0) { + + var objectid = ""; + + if(addResults[0].hasOwnProperty("objectid")){ + objectid = "objectid"; + } + + if(addResults[0].hasOwnProperty("objectId")){ + objectid = "objectId"; + } + + if(addResults[0].hasOwnProperty("OBJECTID")){ + objectid = "OBJECTID"; + } + + // ??? These are the most common objectid values. I may have missed some! + + // Some feature layers will return different casing such as: 'objectid', 'objectId' and 'OBJECTID' + // Normalize these values to the feature type OID so that we don't break other aspects + // of the JS API. + adds[0].attributes[that._esriFieldTypeOID] = addResults[0][objectid]; var graphic = new Graphic(adds[0].geometry,null,adds[0].attributes); layer.add(graphic); } @@ -962,11 +993,12 @@ define([ if( req.status === 200 && req.responseText !== "") { try { - var obj = JSON.parse(this.response); + // var b = this.responseText.replace(/"/g, "'"); // jshint ignore:line + var obj = JSON.parse(this.responseText); callback(obj.addResults, obj.updateResults, obj.deleteResults); } catch(err) { - console.error("EDIT REQUEST REPONSE WAS NOT SUCCESSFUL:", req); + console.error("FAILED TO PARSE EDIT REQUEST RESPONSE:", req); errback("Unable to parse xhr response", req); } }