serialize/deserialize don't need to be public operations

serialize graphics only to JSON object... the 'edit' will be serialized to string
This commit is contained in:
Javier Abadia 2014-01-20 23:07:48 +01:00
parent f3f1d9fc53
commit f7ff11f572
2 changed files with 31 additions and 25 deletions

View File

@ -22,7 +22,7 @@ define(["esri/graphic"],function(Graphic)
var edit = {
operation: operation,
layer: layer,
graphic: this.serialize(graphic)
graphic: this._serialize(graphic)
}
var edits = this._retrieveEditsQueue();
@ -32,7 +32,7 @@ define(["esri/graphic"],function(Graphic)
pendingEditsCount: function()
{
var storedValue = window.localStorage.getItem(this.EDITS_QUEUE_KEY) || "";
var storedValue = window.localStorage.getItem(EDITS_QUEUE_KEY) || "";
if( storedValue == "" )
return 0; // fast easy case
@ -43,7 +43,7 @@ define(["esri/graphic"],function(Graphic)
resetEditsQueue: function()
{
window.localStorage.setItem(this.EDITS_QUEUE_KEY, "");
window.localStorage.setItem(EDITS_QUEUE_KEY, "");
},
popFirstEdit: function()
@ -53,7 +53,7 @@ define(["esri/graphic"],function(Graphic)
canUndoEdit: function()
{
var storedValue = window.localStorage.getItem(this.EDITS_QUEUE_KEY) || "";
var storedValue = window.localStorage.getItem(EDITS_QUEUE_KEY) || "";
return (storedValue != "");
},
@ -76,10 +76,14 @@ define(["esri/graphic"],function(Graphic)
},
//
// internal methods
//
//
// graphic serialization/deserialization
//
serialize: function(graphic)
_serialize: function(graphic)
{
// keep only attributes and geometry, that are the values that get sent to the server by applyEdits()
// see http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Apply_Edits_Feature_Service_Layer/02r3000000r6000000/
@ -90,30 +94,25 @@ define(["esri/graphic"],function(Graphic)
attributes: json.attributes,
geometry: json.geometry
}
var str = JSON.stringify(jsonClean);
return str;
return jsonClean;
},
deserialize: function(str)
_deserialize: function(json)
{
var json = JSON.parse(str);
var graphic = new Graphic(json);
return graphic;
},
//
// internal methods
//
_retrieveEditsQueue: function()
{
var storedValue = window.localStorage.getItem(this.EDITS_QUEUE_KEY) || "";
var storedValue = window.localStorage.getItem(EDITS_QUEUE_KEY) || "";
return this._unpackArrayOfEdits(storedValue);
},
_storeEditsQueue: function(edits)
{
var serializedEdits = this._packArrayOfEdits(edits);
window.localStorage.setItem(this.EDITS_QUEUE_KEY, serializedEdits);
window.localStorage.setItem(EDITS_QUEUE_KEY, serializedEdits);
},
_packArrayOfEdits: function(edits)
@ -123,12 +122,15 @@ define(["esri/graphic"],function(Graphic)
{
serializedEdits.push( JSON.stringify(edit) );
});
return serializedEdits.join(this.SEPARATOR);
return serializedEdits.join(SEPARATOR);
},
_unpackArrayOfEdits: function(serializedEdits)
{
var edits = serializedEdits.split(this.SEPARATOR);
if( !serializedEdits )
return [];
var edits = serializedEdits.split(SEPARATOR);
return edits;
}

View File

@ -71,13 +71,13 @@ describe("Serialize/Deserialize Graphics", function()
it("serialize", function()
{
str = g_graphicsStore.serialize(g_test.pointFeature);
expect(typeof(str)).toBe("string");
str = g_graphicsStore._serialize(g_test.pointFeature);
expect(typeof(str)).toBe("object");
});
it("deserialize", function()
{
graphic = g_graphicsStore.deserialize(str);
graphic = g_graphicsStore._deserialize(str);
expect(typeof(graphic)).toBe("object");
expect(graphic.declaredClass).toEqual("esri.Graphic");
});
@ -109,13 +109,13 @@ describe("Serialize/Deserialize Graphics", function()
it("serialize", function()
{
str = g_graphicsStore.serialize(g_test.lineFeature);
expect(typeof(str)).toBe("string");
str = g_graphicsStore._serialize(g_test.lineFeature);
expect(typeof(str)).toBe("object");
});
it("deserialize", function()
{
graphic = g_graphicsStore.deserialize(str);
graphic = g_graphicsStore._deserialize(str);
expect(typeof(graphic)).toBe("object");
expect(graphic.declaredClass).toEqual("esri.Graphic");
});
@ -147,13 +147,13 @@ describe("Serialize/Deserialize Graphics", function()
it("serialize", function()
{
str = g_graphicsStore.serialize(g_test.polygonFeature);
expect(typeof(str)).toBe("string");
str = g_graphicsStore._serialize(g_test.polygonFeature);
expect(typeof(str)).toBe("object");
});
it("deserialize", function()
{
graphic = g_graphicsStore.deserialize(str);
graphic = g_graphicsStore._deserialize(str);
expect(typeof(graphic)).toBe("object");
expect(graphic.declaredClass).toEqual("esri.Graphic");
});
@ -192,6 +192,10 @@ describe("Edit queue management", function()
{
g_graphicsStore.appendEdit(g_graphicsStore.ADD, 6, g_test.pointFeature);
expect(g_graphicsStore.pendingEditsCount()).toBe(1);
g_graphicsStore.appendEdit(g_graphicsStore.UPDATE, 6, g_test.polygonFeature);
expect(g_graphicsStore.pendingEditsCount()).toBe(2);
g_graphicsStore.appendEdit(g_graphicsStore.DELETE, 6, g_test.lineFeature);
expect(g_graphicsStore.pendingEditsCount()).toBe(3);
});
it("Pops edit from edits queue", function()