Game Development Reference
In-Depth Information
To use transaction, we need to get a reference to our database, then request a trans-
action for a particular data store. Once we have a reference to a data store, we can
perform the various functions related to the data store, such as putting data into it,
reading data from it, updating data, and finally, deleting data from a data store.
var TodoItem = function(task) {
this.task = task;
this.completed = false;
};
try {
var trans = db.transaction(storeName,
"readwrite");
var store = trans.objectStore(storeName);
var task1 = new TodoItem("Buy more pizza");
var task2 = new TodoItem("Finish writing the
book");
var task3 = new TodoItem("Shave before going
to work");
var request = store.put(task1);
// We can reuse this request object to store
multiple objects
request = store.put(task2);
request = store.put(task3);
request.onsuccess = function(e) {
log("Success!" + value.key);
};
request.onerror = function(e) {
log(e.stack);
};
} catch (e) {
log(e.stack);
}
To store an item in our data store we need to follow a couple of steps. Note that
if anything goes wrong during this transaction, we simply catch whatever error is
thrown by the browser, and execution continues uninterrupted because of the try/
catch block.
Search WWH ::




Custom Search