From 0bbf9ca83afb8fa34ca7ff9c1008ec29d0a6b211 Mon Sep 17 00:00:00 2001 From: Javier Abadia Date: Tue, 28 Jan 2014 12:55:08 +0100 Subject: [PATCH] take care of updates for features added offline (replace negative tmp id by final id) still has the problem with the Editor that removes the graphic from the layer after the update --- lib/edit/editsStore.js | 17 +++++++-- lib/edit/offlineFeatureService.js | 2 +- test/SpecRunner.editsStore.html | 5 ++- test/spec/editsStoreSpec.js | 60 +++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/lib/edit/editsStore.js b/lib/edit/editsStore.js index da70681..fc34874 100644 --- a/lib/edit/editsStore.js +++ b/lib/edit/editsStore.js @@ -88,9 +88,22 @@ define(["esri/graphic"], function(Graphic) return null; }, - replaceTempId: function(tempId, objectid) + replaceTempId: function(tempId, newObjectId, objectIdField) { - console.log("temp:",tempId,"objectid:",objectid); + var edits = this._retrieveEditsQueue(); + var replaceCount = 0; + edits.forEach(function(edit) + { + var graphic = this._deserialize(edit.graphic); + if( graphic.attributes[ objectIdField] == tempId ) + { + graphic.attributes[objectIdField] = newObjectId; + edit.graphic = this._serialize(graphic); + replaceCount += 1; + } + },this); + var success = this._storeEditsQueue(edits); + return success? replaceCount : false; }, hasPendingEdits: function() diff --git a/lib/edit/offlineFeatureService.js b/lib/edit/offlineFeatureService.js index 1afeb87..605329c 100644 --- a/lib/edit/offlineFeatureService.js +++ b/lib/edit/offlineFeatureService.js @@ -159,7 +159,7 @@ define([ editsStore.popFirstEdit(); if( addResults ) { - editsStore.replaceTempId(tempId, addResults[0].objectId); + editsStore.replaceTempId(tempId, addResults[0].objectId, layer.objectIdField); } self.emit('edits-sent',arguments); self.replayStoredEdits(); diff --git a/test/SpecRunner.editsStore.html b/test/SpecRunner.editsStore.html index 4b783f0..1aeb6c9 100644 --- a/test/SpecRunner.editsStore.html +++ b/test/SpecRunner.editsStore.html @@ -90,7 +90,10 @@ g_test.pointFeature = new Graphic( g_test.point, g_test.pointSymbol, {"name": "the name of the feature", "objectid":2}); g_test.lineFeature = new Graphic( g_test.line, g_test.lineSymbol, {"nombre": "España","objectid":5}); - g_test.polygonFeature = new Graphic( g_test.polygon, g_test.polygonSymbol, {"nombre": "España","timestamp": new Date().getTime(), "objectid":5}); + g_test.polygonFeature = new Graphic( g_test.polygon, g_test.polygonSymbol, {"nombre": "España","timestamp": new Date().getTime(), "objectid":8}); + + g_test.newPointFeature = new Graphic( g_test.point, g_test.pointSymbol, {"name": "the name of the feature", "objectid":-1}); + g_test.newPointFeatureUpdated = new Graphic( g_test.point, g_test.pointSymbol, {"name": "the name of the feature modified", "objectid":-1}); console.log(g_test); } diff --git a/test/spec/editsStoreSpec.js b/test/spec/editsStoreSpec.js index fd6c2cc..5881c11 100644 --- a/test/spec/editsStoreSpec.js +++ b/test/spec/editsStoreSpec.js @@ -305,6 +305,66 @@ describe("Public Interface", function() }); }); + describe("Replacement of Temporary Ids", function() + { + function getObjectIds() + { + var edits = g_editsStore._retrieveEditsQueue(); + var objectids = edits.map(function(edit) + { + return g_editsStore._deserialize(edit.graphic).attributes.objectid + }); + return objectids; + } + + it("reset edits queue", function() + { + g_editsStore.resetEditsQueue(); + expect(g_editsStore.pendingEditsCount()).toBe(0); + }); + + it("add edits to edits queue", function() + { + var success, objectids; + expect(g_test.newPointFeature.attributes.objectid).toBe(-1); + success = g_editsStore.pushEdit(g_editsStore.ADD, 6, g_test.newPointFeature); + expect(g_editsStore.pendingEditsCount()).toBe(1); + expect(success).toBeTruthy(); + expect(g_editsStore.peekFirstEdit().graphic.attributes.objectid).toBe(-1); + + expect(g_test.polygonFeature.attributes.objectid).toBe(8); + success = g_editsStore.pushEdit(g_editsStore.UPDATE, 3, g_test.polygonFeature); + expect(success).toBeTruthy(); + expect(g_editsStore.pendingEditsCount()).toBe(2); + objectids = getObjectIds(); + expect(objectids).toEqual([-1,8]); + + expect(g_test.lineFeature.attributes.objectid).toBe(5); + success = g_editsStore.pushEdit(g_editsStore.UPDATE, 3, g_test.lineFeature); + expect(success).toBeTruthy(); + expect(g_editsStore.pendingEditsCount()).toBe(3); + objectids = getObjectIds(); + expect(objectids).toEqual([-1,8,5]); + }); + + it("replace ids", function() + { + var replaceCount, objectids; + + objectids = getObjectIds(); + expect(objectids).toEqual([-1,8,5]); + + replaceCount = g_editsStore.replaceTempId(-1,10,"objectid"); + + expect(replaceCount).toBe(1); + objectids = getObjectIds(); + expect(objectids).toEqual([10,8,5]); + + replaceCount = g_editsStore.replaceTempId(-1,10,"objectid"); + expect(replaceCount).toBe(0); + }); + }) + describe("Undo/Redo management", function() { it("reset edits queue", function()