Game Development Reference
In-Depth Information
is one where the specific context of the data store with respect to the
application is taken into account, and each indexed field is carefully
considered. The phrase to keep in mind when designing your data
stores is the following: measure it twice, cut it once.
Although any object can be saved in a data store (as opposed to a
relational database, where the data stored must carefully follow the
table structure, as defined by the table's schema), in order to optim-
ize the performance of your application, try to build your data stores
with the data that it will store in mind. It is true that any data can be
smacked into any data store, but a wise developer considers the data
being stored very carefully before committing it to a database.
Once the data store is set up, and we have at least one meaningful index, we can
start to pull data out of the data store. The easiest way to retrieve objects from a data
store is to use an index, and query for a specific object, as shown in the following
code:
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
// the data store, a simple readonly
transaction will sufice.
var trans = db.transaction(storeName,
"readonly");
var store = trans.objectStore(storeName);
// 2. specify an index to use, and the data
to get from it
var req =
store.index("taskIndex").get(taskName);
req.onsuccess = function(e) {
var todoItem = e.target.result;
Search WWH ::




Custom Search