Created
September 3, 2016 17:33
-
-
Save Breta01/eb86eb28bdeb66b947f576aeb6cd2d66 to your computer and use it in GitHub Desktop.
Implementing IndexedDB
This file contains 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
/** Fetch all of the stats from the datastore **/ | |
mkDB.fetchStats = function(callback, user = "All") { | |
var db = datastore; | |
var tx = db.transaction("stats", "readonly"); | |
var store = tx.objectStore("stats"); | |
//Get only certain user data if specified | |
if (user === "All") { | |
var request = store.openCursor(IDBKeyRange.lowerBound(0)); | |
} else { | |
var index = store.index("by_user"); | |
var request = index.openCursor(IDBKeyRange.only(user)); | |
} | |
// Storing loaded stats | |
var stats = []; | |
request.onsuccess = function() { | |
var rec = request.result; | |
if (rec) { | |
// Called for each matching record. | |
stats.push(rec.value); | |
rec.continue(); | |
} | |
}; | |
tx.oncomplete = function() { | |
callback(stats); | |
}; | |
// Error handling | |
request.onerror = mkDB.onerror; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment