Game Development Reference
In-Depth Information
// todoItem.task => "Buy more pizza"
// todoItem.completed => false
callback(todoItem);
};
req.onerror = function(e) {
// Handle error
};
};
// Search for a TodoItem object with a task
property of "Buy more pizza"
getTask("Buy more pizza", function(taskItem) {
console.log("TaskItem object: " +
taskItem.task);
});
The preceding function attempts to retrieve a single saved object from our data store.
The search is made for an object with its task property that matches the task name
supplied to the function. If one is found, it will be retrieved from the data store, and
passed to the store object's request through the event object passed in to the call-
back function. If an error occurs in the process (for example, if the index supplied
doesn't exist), the onerror event is triggered. Finally, if no objects in the data store
match the search criteria, the resulting property passed in through the request para-
meter object will be null.
Now, to search for multiple items, we can take a similar approach, but instead we
request an IndexedDBCursor object. A cursor is basically a pointer to a particu-
lar result from a result set of zero or more objects. We can use the cursor to iter-
ate through every object in the result set, until the current cursor points at no object
(null), indicating that there are no more objects in the result set.
var TodoItem = function(task) {
this.task = task;
this.completed = false;
};
function getTask(taskName, callback) {
// 1. Open a transaction. Since we don't need
to write anything to
Search WWH ::




Custom Search