9 // https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/
10 var DB_NAME = "example";
11 var OBJ_STORE_NAME = "objects";
15 // Note that open() now returns a Future which resolves for open success.
16 var f = this.indexedDB.open(DB_NAME, VERSION);
18 // Upgradeneeded is still an event since it's conditional (it isn't always
19 // called and therefore can't be used to productively chain), so we
20 // register it as such.
21 f.onupgradeneeded = function(e) {
22 console.log("database creation/upgrade needed");
23 console.log("creating object store:", OBJ_STORE_NAME)
24 e.target.result.createObjectStore(OBJ_STORE_NAME);
27 // Now chain each step of our open/write/read process.
29 // We can only write once the DB is opened and upgraded/created.
30 console.log("database opened successfully");
33 // Create a new Transaction in which to write and retun a future for
34 // when the transaction opens.
35 return db.transaction([OBJ_STORE_NAME], "readwrite").open();
36 }).then(function(trans) {
37 // Get the Object Store via the Transaction and write asynchronously
38 return trans.objectStore(OBJ_STORE_NAME).put({ value: "stuff" }, key);
39 }).then(function(transaction) {
40 console.log("writing successful");
43 return trans.objectStore(OBJ_STORE_NAME).get(key);
44 }).done(function(value) { console.log("reading successful:", value); },
45 function(e) { console.error("Failed with error:", e); }