HTML and CSS Reference
In-Depth Information
var objectStore = tx.objectStore(type);
var request = objectStore.delete(id);
request.onsuccess = function(event) {
request.onerror = function(event) {
errorCallback('object_not_stored', 'It is not possible to delete the object');
As with the save method, we ensure the transaction is opened with the “readwrite” para-
meter. We are also careful not to tell the client we have successfully performed the delete
until the transaction (rather than the request) completes.
The delete method takes advantage of the delete method on objectStore , which conveni-
ently accepts an ID.
Finally, we will implement the findByProperty method. This is one area where you may
expect IndexedDB to shine brightest with its ability to query directly for the relevant res-
ults rather than looping over all results in JavaScript code.
In fact, IndexedDB can perform queries for properties with specific values, but only if an
index has been created on that property. In a situation where any property can form the
basis of a query, this would mean adding indexes to all properties on an object.
We will not add an index in our example, but it is worth knowing how they work. In order
to create an index, we would need to add the following to the onupgradeneeded callback:
objectStore.createIndex("category", " category ", { unique: false });
Indexes can either be unique or non-unique. The index for the category property on the
task object would need to be non-unique, since many tasks can have the same category .
With that in place we could search for all tasks with a specific category by first obtaining a
reference to the index from the object store:
var index = objectStore.index("category");
Next, you would specify the range you wish to search for (e.g. the index value):
var range = IDBKeyRange.only("Work");
and then open a cursor on that just as we saw in the findAll example, except the range
would be passed as a parameter:
index.openCursor(range).onsuccess = function(event) {
Search WWH ::

Custom Search