Game Development Reference
In-Depth Information
function deleteTask(taskId, callback) {
// 1. Open a transaction. Since we definitely
need to change the object
// in the data store, we need proper access
and benefits
var trans = db.transaction(storeName,
"readwrite");
var store = trans.objectStore(storeName);
// 2. specify an index to use, and the data
to get from it
var req = store.delete(taskId);
req.onsuccess = function(e) {
// Do something, then call callback
};
req.onerror = function(e) {
// Handle error
};
};
The difficulty with this first approach is that we need to know the ID of the object. In
some cases, this would involve a prior transaction request where we'd retrieve the
object based on some easier to get data. For example, if we want to delete all tasks
with the attribute of complete set to true, we'd need to query the data store for those
objects first, then use the IDs associated with each result, and use those values in
the transaction where the objects are deleted.
A second way to remove data from the data store is to simply call clear() on the
object store object. Again, the transaction must be set to readwrite. Doing this will
obliterate every last object in the data store, even if they're all of different types as
shown in the following code snippet:
var trans = db.transaction(storeName,
"readwrite");
var store = trans.objectStore(storeName);
var req = store.clear();
req.onsuccess = function(e) {
// Do something, then call callback
Search WWH ::




Custom Search