HTML and CSS Reference
In-Depth Information
What if you wanted to get all the videos out of the store? Using
the get method won't cut it. We need to iterate through the
entire data store:
var transaction = db.transaction(['blockbusters'],READ_WRITE),
store = transaction.objectStore('blockbusters'),
data = [];
var request = store.openCursor();
request.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
// value is the stored object
data.push(cursor.value);
// get the next object
cursor.continue();
} else {
// we've got all the data now, call
// a success callback and pass the
// data object in.
}
};
In this code block, we're opening up our object store as usual,
but instead of executing a get we open a cursor. This allows us
to cycle through each stored object. We could easily use this
process to find all the videos with a rating of five or more stars
by adding a nested check against cusor.value.rating before
pushing the current stored object onto our data array of results.
For example:
function find(filter, callback) {
// READ_WRITE was declared earlier on in our code
var transaction = db.transaction(['blockbusters'],
¬ READ_WRITE),
store = transaction.objectStore('blockbusters'),
data = [];
var request = store.openCursor();
request.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
if (filter(cursor.value) === true) {
Search WWH ::




Custom Search