HTML and CSS Reference
In-Depth Information
With this we have now given the index a name (the first argu-
ment), and then the name of the property (in our case, 'director')
we want indexed when new objects are stored. Finally, we'll
allow more than one film by the same director by indicating that
we don't expect the values to be unique.
This all means I can store and easily retrieve a blockbuster entry
looking like this:
title: “Belly Dance Bruce - Final Strike”,
date: (new Date).getTime(), // released TODAY!
director: “Bruce Awesome”,
length: 169, // in minutes
rating: 10,
cover: “/images/wobble.jpg”
So let's add some videos to our blockbuster collection.
Adding and putting objects in stores
Okay, “adding and putting.” No doubt you're thinking: ambigu-
ous! There are two methods for inserting data: add and put . The
first adds new data and requires that the data not exist in the
first place. The second puts an updated object in the store, and
if the object isn't already stored, it will insert it as new.
For the purpose of our video store database, we're going to throw
caution to the wind, and just use put. If you're not feeling as callous
as I am, and there's a risk of duplicate objects (if, say, the title was
the same but another field was different, it would leave us with two
objects when we're only expecting one for each title), be sure you
use add and put with appropriate validation and checks.
var video = {
title: “Belly Dance Bruce - Final Strike”,
date: (new Date).getTime(),
director: “Bruce Awesome”,
length: 169,
rating: 10,
cover: “/images/wobble.jpg” },
var transaction = db.transaction(['blockbusters'],READ_WRITE),
store = transaction.objectStore('blockbusters'),
request = store.put(video);
Search WWH ::

Custom Search