Skip to content

Instantly share code, notes, and snippets.

@Breta01
Last active September 3, 2016 18:37
Show Gist options
  • Save Breta01/954286dceb1b31cc37f657af8dcb06fc to your computer and use it in GitHub Desktop.
Save Breta01/954286dceb1b31cc37f657af8dcb06fc to your computer and use it in GitHub Desktop.
Implementing IndexedDB
/** Create a new stat **/
mkDB.createStat = function(title, score, callback) {
var db = datastore;
var transaction = db.transaction(['stats'], 'readwrite');
var objStore = transaction.objectStore('stats');
// Create a timestamp for the stat item. (this is key value)
var timestamp = new Date().getTime();
// Create an object for the stat
var stat = {
game: title,
score: score,
timestamp: timestamp
};
// Create the datastore request.
var request = objStore.put(stat);
// Handle a successful datastore put.
request.onsuccess = function(e) {
callback();
};
// Handle errors.
request.onerror = mkDB.onerror;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment