HTML and CSS Reference
In-Depth Information
}
};
}
The initial portion of this should be very familiar. We create a transaction (although we do
not specify “readwrite” this time).
var tx = database.transaction(type);
var objectStore = tx.objectStore(type);
Once we have a transaction we specify the object store we wish to access and open a cursor
on it. The openCursor method will implicitly create a cursor with all objects in the object
store in its result set.
It is also possible to limit the result set returned by openCursor to a specific key range.
In order to loop through the contents of the cursor we registered a callback with the onsuc-
cess event of the openCursor method. This will be called when the cursor is open and ready
to be interacted with.
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
result.push(cursor.value);
cursor.continue();
} else {
successCallback(result);
}
};
Within this function we have access to a cursor object. If this is null then there are no fur-
ther entries in the result set. If it is not null we can call cursor.value to get the object at the
current cursor position, and then cursor.continue to move the cursor to the next position in
the result set.
When the cursor is eventually null we know we can return any results we have collected
from the cursor to the client. There is no need to wait for the transaction to complete before
returning results to the client because the transaction is not modifying the state of the data-
base.
The next method we will implement is findById . This will highlight another feature of In-
dexedDB; it is trivial to access an object with a specific ID inside an object store without
Search WWH ::




Custom Search