Last active
November 16, 2016 04:00
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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