fixed db objectid scope

This commit is contained in:
Andy Gup 2015-03-25 10:33:30 -06:00
parent ea8b451d81
commit ef07dfe75d
2 changed files with 95 additions and 96 deletions

View File

@ -10,9 +10,7 @@ O.esri.Edit.EditStore = function () {
this.objectStoreName = "features";
this.objectId = "objectid"; // set this depending on how your feature service is configured;
var dbName = this.dbName;
var objectStoreName = this.objectStoreName;
var objectId = this.objectId;
var _dbIndex = "featureId"; // @private
@ -43,14 +41,14 @@ O.esri.Edit.EditStore = function () {
this.pushEdit = function (operation, layerUrl, graphic, callback) {
var edit = {
id: layerUrl + "/" + graphic.attributes[objectId],
id: layerUrl + "/" + graphic.attributes[this.objectId],
operation: operation,
layer: layerUrl,
type: graphic.geometry.type,
graphic: graphic.toJson()
};
var transaction = this._db.transaction([objectStoreName], "readwrite");
var transaction = this._db.transaction([this.objectStoreName], "readwrite");
transaction.oncomplete = function (event) {
callback(true);
@ -60,7 +58,7 @@ O.esri.Edit.EditStore = function () {
callback(false, event.target.error.message);
};
var objectStore = transaction.objectStore(objectStoreName);
var objectStore = transaction.objectStore(this.objectStoreName);
objectStore.put(edit);
};
@ -102,7 +100,7 @@ O.esri.Edit.EditStore = function () {
if (success && typeof result !== "undefined") {
var objectStore = db.transaction([objectStoreName], "readwrite").objectStore(objectStoreName);
var objectStore = db.transaction([this.objectStoreName], "readwrite").objectStore(this.objectStoreName);
// Make a copy of the object
for (var key in dataObject) {
@ -124,7 +122,7 @@ O.esri.Edit.EditStore = function () {
}
else {
var transaction = db.transaction([objectStoreName], "readwrite");
var transaction = db.transaction([this.objectStoreName], "readwrite");
transaction.oncomplete = function (event) {
callback(true, null);
@ -134,7 +132,7 @@ O.esri.Edit.EditStore = function () {
callback(false, event.target.error.message);
};
var objectStore = transaction.objectStore(objectStoreName);
var objectStore = transaction.objectStore(this.objectStoreName);
// Protect against data cloning errors since we don't validate the input object
// Example: if you attempt to use an esri.Graphic in its native form you'll get a data clone error
@ -145,7 +143,7 @@ O.esri.Edit.EditStore = function () {
callback(false, JSON.stringify(err));
}
}
});
}.bind(this));
};
/**
@ -156,7 +154,7 @@ O.esri.Edit.EditStore = function () {
console.assert(this._db !== null, "indexeddb not initialized");
var objectStore = this._db.transaction([objectStoreName], "readwrite").objectStore(objectStoreName);
var objectStore = this._db.transaction([this.objectStoreName], "readwrite").objectStore(this.objectStoreName);
//Get the entry associated with the graphic
var objectStoreGraphicRequest = objectStore.get(this.FEATURE_LAYER_JSON_ID);
@ -223,31 +221,31 @@ O.esri.Edit.EditStore = function () {
// Step 1 - lets see if record exits. If it does not then return callback. Otherwise,
// continue on with the deferred.
self.editExists(id).then(function (result) {
if (result && result.success) {
if (result && result.success) {
var objectStore = db.transaction([objectStoreName], "readwrite").objectStore(objectStoreName);
var objectStore = db.transaction([self.objectStoreName], "readwrite").objectStore(self.objectStoreName);
// Step 2 - go ahead and delete graphic
var objectStoreDeleteRequest = objectStore.delete(id);
// Step 2 - go ahead and delete graphic
var objectStoreDeleteRequest = objectStore.delete(id);
// Step 3 - We know that the onsuccess will always fire unless something serious goes wrong.
// So we go ahead and resolve the deferred here.
objectStoreDeleteRequest.onsuccess = function () {
deferred.resolve(true);
};
// Step 3 - We know that the onsuccess will always fire unless something serious goes wrong.
// So we go ahead and resolve the deferred here.
objectStoreDeleteRequest.onsuccess = function () {
deferred.resolve(true);
};
objectStoreDeleteRequest.onerror = function (msg) {
deferred.reject({success: false, error: msg});
}
objectStoreDeleteRequest.onerror = function (msg) {
deferred.reject({success: false, error: msg});
}
else {
deferred.reject({success: false, message: "id does not exist"})
}
},
// If there is an error in editExists()
function (err) {
deferred.reject({success: false, message: err});
});
}
else {
deferred.reject({success: false, message: "id does not exist"})
}
},
// If there is an error in editExists()
function (err) {
deferred.reject({success: false, message: err});
}.bind(this));
})
};
@ -259,16 +257,16 @@ O.esri.Edit.EditStore = function () {
*/
this.pushPhantomGraphic = function (graphic, callback) {
console.assert(this._db !== null, "indexeddb not initialized");
console.log("HAHAHAHA " + graphic.attributes[objectId] + ", " + graphic.attributes.objectId);
var db = this._db;
var id = this.PHANTOM_GRAPHIC_PREFIX + this._PHANTOM_PREFIX_TOKEN + graphic.attributes[objectId];
var id = this.PHANTOM_GRAPHIC_PREFIX + this._PHANTOM_PREFIX_TOKEN + graphic.attributes[this.objectId];
var object = {
id: id,
graphic: graphic.toJson()
}
var transaction = db.transaction([objectStoreName], "readwrite");
var transaction = db.transaction([this.objectStoreName], "readwrite");
transaction.oncomplete = function (event) {
callback(true, null);
@ -278,7 +276,7 @@ O.esri.Edit.EditStore = function () {
callback(false, event.target.error.message);
};
var objectStore = transaction.objectStore(objectStoreName);
var objectStore = transaction.objectStore(this.objectStoreName);
objectStore.put(object);
};
@ -295,8 +293,8 @@ O.esri.Edit.EditStore = function () {
var phantomGraphicPrefix = this.PHANTOM_GRAPHIC_PREFIX;
var transaction = this._db.transaction([objectStoreName])
.objectStore(objectStoreName)
var transaction = this._db.transaction([this.objectStoreName])
.objectStore(this.objectStoreName)
.openCursor();
transaction.onsuccess = function (event) {
@ -336,8 +334,8 @@ O.esri.Edit.EditStore = function () {
var phantomGraphicPrefix = this.PHANTOM_GRAPHIC_PREFIX;
var transaction = this._db.transaction([objectStoreName])
.objectStore(objectStoreName)
var transaction = this._db.transaction([this.objectStoreName])
.objectStore(this.objectStoreName)
.openCursor();
transaction.onsuccess = function (event) {
@ -405,7 +403,7 @@ O.esri.Edit.EditStore = function () {
callback(false, err);
});
var objectStore = db.transaction([objectStoreName], "readwrite").objectStore(objectStoreName);
var objectStore = db.transaction([self.objectStoreName], "readwrite").objectStore(self.objectStoreName);
// Step 2 - go ahead and delete graphic
var objectStoreDeleteRequest = objectStore.delete(id);
@ -438,8 +436,8 @@ O.esri.Edit.EditStore = function () {
var db = this._db;
var errors = 0;
var tx = db.transaction([objectStoreName], "readwrite");
var objectStore = tx.objectStore(objectStoreName);
var tx = db.transaction([this.objectStoreName], "readwrite");
var objectStore = tx.objectStore(this.objectStoreName);
objectStore.onerror = function () {
errors++;
@ -454,7 +452,6 @@ O.esri.Edit.EditStore = function () {
if (responseObject.hasOwnProperty(key)) {
var edit = responseObject[key];
var id = this.PHANTOM_GRAPHIC_PREFIX + this._PHANTOM_PREFIX_TOKEN + edit.id;
console.log("EDIT " + JSON.stringify(edit))
// CAUTION:
// TO-DO we do NOT match the edit.id with edit's objectId
@ -500,8 +497,8 @@ O.esri.Edit.EditStore = function () {
if (array != []) {
var errors = 0;
var tx = db.transaction([objectStoreName], "readwrite");
var objectStore = tx.objectStore(objectStoreName);
var tx = db.transaction([this.objectStoreName], "readwrite");
var objectStore = tx.objectStore(this.objectStoreName);
objectStore.onerror = function () {
errors++;
@ -519,7 +516,7 @@ O.esri.Edit.EditStore = function () {
else {
callback(true);
}
});
}.bind(this));
};
/**
@ -530,7 +527,7 @@ O.esri.Edit.EditStore = function () {
this.getEdit = function(id,callback){
console.assert(this._db !== null, "indexeddb not initialized");
var objectStore = this._db.transaction([objectStoreName], "readwrite").objectStore(objectStoreName);
var objectStore = this._db.transaction([this.objectStoreName], "readwrite").objectStore(this.objectStoreName);
require(["dojo/Deferred"], function (Deferred) {
@ -571,8 +568,8 @@ O.esri.Edit.EditStore = function () {
var fLayerJSONId = this.FEATURE_LAYER_JSON_ID;
var phantomGraphicPrefix = this.PHANTOM_GRAPHIC_PREFIX;
var transaction = this._db.transaction([objectStoreName])
.objectStore(objectStoreName)
var transaction = this._db.transaction([this.objectStoreName])
.objectStore(this.objectStoreName)
.openCursor();
transaction.onsuccess = function (event) {
@ -612,8 +609,8 @@ O.esri.Edit.EditStore = function () {
var fLayerJSONId = this.FEATURE_LAYER_JSON_ID;
var phantomGraphicPrefix = this.PHANTOM_GRAPHIC_PREFIX;
var transaction = this._db.transaction([objectStoreName])
.objectStore(objectStoreName)
var transaction = this._db.transaction([this.objectStoreName])
.objectStore(this.objectStoreName)
.openCursor();
transaction.onsuccess = function (event) {
@ -651,10 +648,10 @@ O.esri.Edit.EditStore = function () {
console.assert(this._db !== null, "indexeddb not initialized");
var objectStore = this._db.transaction([objectStoreName], "readwrite").objectStore(objectStoreName);
var objectStore = this._db.transaction([this.objectStoreName], "readwrite").objectStore(this.objectStoreName);
//Let's get the entry associated with the graphic
var objectStoreGraphicRequest = objectStore.get(graphic.attributes.objectid);
var objectStoreGraphicRequest = objectStore.get(graphic.attributes[this.objectId]);
objectStoreGraphicRequest.onsuccess = function () {
//Grab the data object returned as a result
@ -662,7 +659,7 @@ O.esri.Edit.EditStore = function () {
//Create a new update object
var update = {
id: layer + "/" + graphic.attributes.objectid,
id: layer + "/" + graphic.attributes[this.objectId],
operation: operation,
layer: layer,
graphic: graphic.toJson()
@ -704,50 +701,50 @@ O.esri.Edit.EditStore = function () {
var deferred = null;
var self = this;
var id = layerUrl + "/" + graphic.attributes.objectid;
var id = layerUrl + "/" + graphic.attributes[this.objectId];
require(["dojo/Deferred"], function (Deferred) {
deferred = new Deferred();
// Step 1 - lets see if record exits. If it does then return callback.
self.editExists(id).then(function (result) {
if (result.success) {
// Step 4 - Then we check to see if the record actually exists or not.
deferred.then(function (result) {
if (result.success) {
// Step 4 - Then we check to see if the record actually exists or not.
deferred.then(function (result) {
// IF the delete was successful, then the record should return 'false' because it doesn't exist.
self.editExists(id).then(function (results) {
results.success == false ? callback(true) : callback(false);
},
function (err) {
callback(true); //because we want this test to throw an error. That means item deleted.
})
},
// There was a problem with the delete operation on the database
function (err) {
callback(false, err);
});
// IF the delete was successful, then the record should return 'false' because it doesn't exist.
self.editExists(id).then(function (results) {
results.success == false ? callback(true) : callback(false);
},
function (err) {
callback(true); //because we want this test to throw an error. That means item deleted.
})
},
// There was a problem with the delete operation on the database
function (err) {
callback(false, err);
});
var objectStore = db.transaction([objectStoreName], "readwrite").objectStore(objectStoreName);
var objectStore = db.transaction([self.objectStoreName], "readwrite").objectStore(self.objectStoreName);
// Step 2 - go ahead and delete graphic
var objectStoreDeleteRequest = objectStore.delete(id);
// Step 2 - go ahead and delete graphic
var objectStoreDeleteRequest = objectStore.delete(id);
// Step 3 - We know that the onsuccess will always fire unless something serious goes wrong.
// So we go ahead and resolve the deferred here.
objectStoreDeleteRequest.onsuccess = function () {
deferred.resolve(true);
};
// Step 3 - We know that the onsuccess will always fire unless something serious goes wrong.
// So we go ahead and resolve the deferred here.
objectStoreDeleteRequest.onsuccess = function () {
deferred.resolve(true);
};
objectStoreDeleteRequest.onerror = function (msg) {
deferred.reject({success: false, error: msg});
}
objectStoreDeleteRequest.onerror = function (msg) {
deferred.reject({success: false, error: msg});
}
},
// If there is an error in editExists()
function (err) {
callback(false);
});
}
},
// If there is an error in editExists()
function (err) {
callback(false);
});
});
};
@ -761,8 +758,8 @@ O.esri.Edit.EditStore = function () {
this.resetEditsQueue = function (callback) {
console.assert(this._db !== null, "indexeddb not initialized");
var request = this._db.transaction([objectStoreName], "readwrite")
.objectStore(objectStoreName)
var request = this._db.transaction([this.objectStoreName], "readwrite")
.objectStore(this.objectStoreName)
.clear();
request.onsuccess = function (event) {
setTimeout(function () {
@ -781,8 +778,8 @@ O.esri.Edit.EditStore = function () {
var id = this.FEATURE_LAYER_JSON_ID;
var phantomGraphicPrefix = this.PHANTOM_GRAPHIC_PREFIX;
var transaction = this._db.transaction([objectStoreName], "readwrite")
var objectStore = transaction.objectStore(objectStoreName);
var transaction = this._db.transaction([this.objectStoreName], "readwrite")
var objectStore = transaction.objectStore(this.objectStoreName);
objectStore.openCursor().onsuccess = function (evt) {
var cursor = evt.target.result;
@ -810,11 +807,12 @@ O.esri.Edit.EditStore = function () {
var db = this._db;
var deferred = null;
var self = this;
require(["dojo/Deferred"], function (Deferred) {
deferred = new Deferred();
var objectStore = db.transaction([objectStoreName], "readwrite").objectStore(objectStoreName);
var objectStore = db.transaction([self.objectStoreName], "readwrite").objectStore(self.objectStoreName);
//Get the entry associated with the graphic
var objectStoreGraphicRequest = objectStore.get(id);
@ -851,8 +849,8 @@ O.esri.Edit.EditStore = function () {
var usage = {sizeBytes: 0, editCount: 0};
var transaction = this._db.transaction([objectStoreName])
.objectStore(objectStoreName)
var transaction = this._db.transaction([this.objectStoreName])
.objectStore(this.objectStoreName)
.openCursor();
console.log("dumping keys");
@ -914,7 +912,7 @@ O.esri.Edit.EditStore = function () {
this.init = function (callback) {
console.log("init editsStore.js");
var request = indexedDB.open(dbName, 11);
var request = indexedDB.open(this.dbName, 11);
callback = callback || function (success) {
console.log("EditsStore::init() success:", success);
}.bind(this);
@ -927,11 +925,11 @@ O.esri.Edit.EditStore = function () {
request.onupgradeneeded = function (event) {
var db = event.target.result;
if (db.objectStoreNames.contains(objectStoreName)) {
db.deleteObjectStore(objectStoreName);
if (db.objectStoreNames.contains(this.objectStoreName)) {
db.deleteObjectStore(this.objectStoreName);
}
var objectStore = db.createObjectStore(objectStoreName, {keyPath: "id"});
var objectStore = db.createObjectStore(this.objectStoreName, {keyPath: "id"});
objectStore.createIndex(_dbIndex, _dbIndex, {unique: false});
}.bind(this);

View File

@ -53,6 +53,7 @@
g_offlineFeaturesManager = new O.esri.Edit.OfflineFeaturesManager();
g_offlineFeaturesManager.DB_NAME = "FEATURES_TEST";
g_editsStore = new O.esri.Edit.EditStore();
g_editsStore.dbName = "FEATURES_TEST";
g_map = new Map("map", {
basemap: "satellite",