push, get, update, unit test FeatureLayerData object

This commit is contained in:
Andy Gup 2015-02-27 16:51:17 -07:00
parent b377b999f6
commit 8177636fde
2 changed files with 140 additions and 3 deletions

View File

@ -17,6 +17,8 @@ O.esri.Edit.EditStore = function()
this.UPDATE = "update";
this.DELETE = "delete";
this.FEATURE_LAYER_DATAOBJECT = "feature-layer-object-1001";
this.isSupported = function()
{
if(!window.indexedDB){
@ -56,6 +58,89 @@ O.esri.Edit.EditStore = function()
objectStore.put(edit);
};
/**
* Use this to store any data related to your app that will assist in restoring
* a FeatureLayer.
* @param dataObject
* @param callback {true, null} or {false, error}
*/
this.pushFeatureLayerData = function(dataObject, callback){
console.assert(this._db !== null, "indexeddb not initialized");
dataObject.id = this.FEATURE_LAYER_DATAOBJECT;
var transaction = this._db.transaction([objectStoreName],"readwrite");
transaction.oncomplete = function(event){
callback(true, null);
};
transaction.onerror = function(event){
callback(false,event.target.error.message);
};
var objectStore = transaction.objectStore(objectStoreName);
objectStore.put(dataObject);
};
/**
* Retrieve the FeatureLayer data object
* @param callback {true, object} or {false, error}
*/
this.getFeatureLayerData = function(callback){
console.assert(this._db !== null, "indexeddb not initialized");
var objectStore = this._db.transaction([objectStoreName],"readwrite").objectStore(objectStoreName);
//Get the entry associated with the graphic
var objectStoreGraphicRequest = objectStore.get(this.FEATURE_LAYER_DATAOBJECT);
objectStoreGraphicRequest.onsuccess = function() {
var object = objectStoreGraphicRequest.result;
callback(true,object);
};
objectStoreGraphicRequest.onerror = function(msg){
callback(false,msg);
}
};
/**
* Update the FeatureLayer Data object
* @param object
* @param callback {true, null} or {false, error}
*/
this.updateFeatureLayerData = function(object,callback){
console.assert(this._db !== null, "indexeddb not initialized");
this.getFeatureLayerData(function(success,result){
if(success){
var objectStore = this._db.transaction([objectStoreName],"readwrite").objectStore(objectStoreName);
for(var key in object){
if (object.hasOwnProperty(key)) {
result[key] = object[key];
}
}
// Insert the update into the database
var updateGraphicRequest = objectStore.put(result);
updateGraphicRequest.onsuccess = function(){
callback(true,null);
}
updateGraphicRequest.onerror = function(err){
callback(false,err);
};
}
})
};
/**
* Returns all the edits recursively via the callback
* @param callback {value, message}
@ -65,6 +150,9 @@ O.esri.Edit.EditStore = function()
console.assert(this._db !== null, "indexeddb not initialized");
if(this._db !== null){
var id = this.FEATURE_LAYER_DATAOBJECT;
var transaction = this._db.transaction([objectStoreName])
.objectStore(objectStoreName)
.openCursor();
@ -73,7 +161,9 @@ O.esri.Edit.EditStore = function()
{
var cursor = event.target.result;
if(cursor && cursor.hasOwnProperty("value") && cursor.value.hasOwnProperty("id")){
callback(cursor.value,null);
if(cursor.value.id !== id){
callback(cursor.value,null);
}
cursor.continue();
}
else
@ -102,6 +192,9 @@ O.esri.Edit.EditStore = function()
var editsArray = [];
if(this._db !== null){
var id = this.FEATURE_LAYER_DATAOBJECT;
var transaction = this._db.transaction([objectStoreName])
.objectStore(objectStoreName)
.openCursor();
@ -110,7 +203,10 @@ O.esri.Edit.EditStore = function()
{
var cursor = event.target.result;
if(cursor && cursor.hasOwnProperty("value") && cursor.value.hasOwnProperty("id")){
editsArray.push(cursor.value);
if(cursor.value.id !== id){
editsArray.push(cursor.value);
}
cursor.continue();
}
else
@ -260,6 +356,7 @@ O.esri.Edit.EditStore = function()
console.assert(this._db !== null, "indexeddb not initialized");
var count = 0;
var id = this.FEATURE_LAYER_DATAOBJECT;
var objectStore = this._db.transaction([objectStoreName]).objectStore(objectStoreName);
objectStore.openCursor().onsuccess = function(evt)
@ -267,7 +364,9 @@ O.esri.Edit.EditStore = function()
var cursor = evt.target.result;
if(cursor && cursor.hasOwnProperty("value") && cursor.value.hasOwnProperty("id"))
{
count++;
if(cursor.value.id !== id){
count++;
}
cursor.continue();
}
else

View File

@ -572,6 +572,44 @@ describe("Offline Editing", function()
})
});
async.it("store FeatureLayer data", function(done){
var dataObject = {
graphics: {test:1},
renderer: {test:2}
}
g_editsStore.pushFeatureLayerData(dataObject,function(result){
expect(result).toBe(true);
done();
});
});
async.it("retrieve FeatureLayer data", function(done){
g_editsStore.getFeatureLayerData(function(success,data){
expect(success).toBe(true);
expect(data.id).toBe(g_editsStore.FEATURE_LAYER_DATAOBJECT);
expect(typeof data.graphics).toBe("object");
expect(typeof data.renderer).toBe("object");
done();
});
});
async.it("update FeatureLayer data", function(done){
var dataObject = {
graphics: {test: 2}
}
g_editsStore.updateFeatureLayerData(dataObject,function(success,result){
expect(success).toBe(true);
expect(result).toBe(null);
g_editsStore.getFeatureLayerData(function(success,data){
expect(success).toBe(true);
expect(data.id).toBe(g_editsStore.FEATURE_LAYER_DATAOBJECT);
expect(data.graphics.test).toBe(2);
done();
});
})
});
async.it("go Online", function(done)
{
// Remember we deleted g3! So our total count is 8 not 9. HOWEVER, there should be 9 records in the database!