Skip to content

Instantly share code, notes, and snippets.

@luke-denton-aligent
Last active November 16, 2016 04:00
Show Gist options
  • Save luke-denton-aligent/fceb976163412cb74f678185a416ccf1 to your computer and use it in GitHub Desktop.
Save luke-denton-aligent/fceb976163412cb74f678185a416ccf1 to your computer and use it in GitHub Desktop.
This snippet shows how to user cursors with IndexedDB to loop through results
// Offline Web Applications course on Udacity https://classroom.udacity.com/courses/ud899
//Starting with "Group data together using indexes" snippet in https://gist.github.com/luke-denton/193fd42e9fcfe6e2f1e8b6c4de58dc4e
//Basically a complicated way of calling getAll()
dbPromise.then(function(db) {
var tx = db.transaction('people');
var peopleStore = tx.objectStore('people');
var superheroIndex = peopleStore.index('superhero');
return superheroIndex.openCursor();
}).then(function() { //Showing how to skip items in the loop
return cursor.advance(2); //This will skip the first 2 items in the results, so they won't be changed/deleted etc, just returned as they are
}).then(function logPerson(cursor) {
if (!cursor) return;
//Potential Actions
//cursor.update(newValue); //Change the value of the object that the cursor is pointing to
//cursor.delete(); //Delete the item that the cursor is pointing to
//cursor.continue() returns a Promise for a cursor representing the next item
//Passing logPerson into the then() function means the cursor for the next item will be passed into
//this function and processed
return cursor.continue().then(logPerson);
}).then(function() {
//Done cursoring
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment