Reputation: 10745
I have an IndexedDB data store with a few hundred objects in it. I'd like to grab items 40-59 from it based on the ordering in one of my indexes on that store. Is there a way to do that without simply calling cursor.continue() 39 times before starting to consume data? It seems pretty wasteful in terms of processing time.
Upvotes: 6
Views: 2682
Reputation: 221
I had the same problem and cursor.advance(40)
is what you want to use.
One thing that took me a while to figure out so may be useful to others is if you want to advance the cursor and iterate through the results you will either need to call them in separate openCursor().onsuccess
handlers, or implement some kind of tracking to prevent them both being called in the same request or an InvalidStateError
exception with be thrown. This can be done like so:
Separate Handlers
// advance first
store.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
cursor.advance(40);
};
// then iterate
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
cursor.continue();
});
Same Handler
// create flag for advancing
var advancing = true;
store.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
// advancing
if (advancing === true) {
cursor.advance(40);
// set advancing flag to false so we don't advance again
advancing = false;
}
// continuing
else {
cursor.continue();
}
}
Spec reference: http://www.w3.org/TR/IndexedDB/#widl-IDBCursor-advance-void-unsigned-long-count MDN Reference with example: https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor.advance
Upvotes: 8
Reputation: 661
I believe you can call cursor.advance(40)
Spec reference: http://www.w3.org/TR/IndexedDB/#widl-IDBCursor-advance
Upvotes: 2