Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / Promises / reworked_APIs / IndexedDB / example / before.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
14       var db = null;
15
16       var onDBWriteable = function(e) {
17         // Create a new Transaction in which to write
18         var trans = db.transaction([OBJ_STORE_NAME], "readwrite");
19         // Get the Object Store via the Transaction
20         var store = trans.objectStore(OBJ_STORE_NAME);
21
22         // Write asynchronously
23         var key = "thinger"
24         var putRequest = store.put({ value: "stuff" }, key);
25         putRequest.onsuccess = function(e) {
26           console.log("writing successful");
27
28           // Read it back
29           var getRequest = store.get(key);
30           getRequest.onsuccess = function(e) {
31             console.log("reading successful:", e.target.result);
32           };
33         };
34       };
35
36       // Open up a DB against a particular version
37
38       // Note that this call is "async" in the sense that the request
39       // deterministically fires the "upgradeneeded" and "success" events in a
40       // later turn, giving us a chance to regsiter handlers here. There's
41       // nothing in the API contract to indicat that this is so, however.
42       var openRequest = this.indexedDB.open(DB_NAME, VERSION);
43
44       // If this is the first time we run, upgradeneeded is thrown and we can
45       // create the object store.
46       openRequest.onupgradeneeded = function (e) {
47         console.log("database creation/upgrade needed");
48         db = e.target.result;
49
50         console.log("creating object store:", OBJ_STORE_NAME)
51         // Note that we can only call createObjectStore inside of an
52         // upgradeneeded event.
53         db.createObjectStore(OBJ_STORE_NAME);
54       };
55
56       // We can only write once the DB is opened and upgraded/created. onsuccess
57       // is always called after onupgradeneeded (if upgradeneeded is true).
58       openRequest.onsuccess = function(e) {
59         console.log("database opened successfully");
60         db = e.target.result;
61         console.log(db);
62
63         // Now write something to the Object Store
64         onDBWriteable(e);
65       };
66
67     </script>
68   </head>
69   <body></body>
70 </html>