Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / third_party / Promises / reworked_APIs / IndexedDB / example / after.html
1 <!DOCTYPE html>
2 <html>
3   <head>
4     <title></title>
5     <script>
6       "use strict";
7
8       // Cribbed from:
9       //   https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/
10       var DB_NAME = "example";
11       var OBJ_STORE_NAME = "objects";
12       var VERSION = 1.0;
13       var key = "thinger"
14
15       // Note that open() now returns a Future which resolves for open success.
16       var f = this.indexedDB.open(DB_NAME, VERSION);
17
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);
25       };
26
27       // Now chain each step of our open/write/read process.
28       f.then(function(db) {
29         // We can only write once the DB is opened and upgraded/created.
30         console.log("database opened successfully");
31         console.log(db);
32
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");
41
42           // Read it back
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); }
46       );
47     </script>
48   </head>
49   <body></body>
50 </html>