Game Development Reference
In-Depth Information
};
req.onerror = function(e) {
// Handle error
};
};
// Retrieve every TodoItem in the data store
var todoItems = new Array();
getTask("Buy more pizza", function() {
for (var i = 0; i < todoItems.length; i++) {
console.log("TaskItem object: " +
todoItems[i].task);
}
})
You will note a few things with the above code snippet. First, any object that goes into
our IndexedDB data store is stripped of its DNA, and only a simple hash is stored in
its stead. Thus, if the prototype information of each object we retrieve from the data
store is important to the application, we will need to manually reconstruct each object
from the data that we get back from the data store.
Second, observe that we can filter the subset of the data store that we would like to
take out of it. This is done with an IndexedDB Key Range object, which specifies the
offset from which to start fetching data. In our case, we specified a lower bound of
zero, meaning that the lowest primary key value we want is zero. In other words, this
particular query requests all of the records in the data store.
Finally, remember that the result from the request is not a single result or an array of
results. Instead, all of the results are returned one at a time in the form of a cursor.
We can check for the presence of a cursor altogether, then use the cursor if one is
indeed present. Then, the way we request the next cursor is by calling the contin-
ue() function on the cursor itself.
Another way to think of cursors is by imagining a spreadsheet application. Pretend
that the 10 objects returned from our request each represent a row in this spread-
sheet. So IndexedDB will fetch all 10 of those objects to memory, and send a pointer
to the first result through the event.target.result property in the onsuccess
callback. By calling cursor.continue() , we simply tell IndexedDB to now give us
a reference to the next object in the result set (or, in other words, we ask for the next
Search WWH ::




Custom Search