1 | <html> |
---|
2 | <head> |
---|
3 | <script src="../../../dojo/dojo.js"></script> |
---|
4 | <script src="../storage-browser.js"></script> |
---|
5 | |
---|
6 | <script> |
---|
7 | dojo.require("dojox.storage"); |
---|
8 | |
---|
9 | function runDemo(){ |
---|
10 | // setup event handlers |
---|
11 | dojo.byId("saveButton").onclick = saveValue; |
---|
12 | |
---|
13 | // write out what our storage provider is for debugging |
---|
14 | dojo.byId("currentProvider").innerHTML = |
---|
15 | dojox.storage.manager.currentProvider.declaredClass; |
---|
16 | |
---|
17 | loadValues(); |
---|
18 | } |
---|
19 | |
---|
20 | function loadValues(){ |
---|
21 | // get any values that were saved before and write them into the page |
---|
22 | var results = dojox.storage.get("myValues"); |
---|
23 | |
---|
24 | if(results){ |
---|
25 | var printMe = "<ul>"; |
---|
26 | for(var i = 0; i < results.length; i++){ |
---|
27 | printMe += "<li>" + results[i] + "</li>"; |
---|
28 | } |
---|
29 | printMe += "</ul>"; |
---|
30 | dojo.byId("allValues").innerHTML = printMe; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | function saveValue(){ |
---|
35 | var value = dojo.byId("saveValue").value; |
---|
36 | if(value == undefined || value === ""){ |
---|
37 | alert("Please enter a correct value"); |
---|
38 | return; |
---|
39 | } |
---|
40 | |
---|
41 | // get the old values first, since we are saving everything |
---|
42 | // as one key |
---|
43 | var results = dojox.storage.get("myValues"); |
---|
44 | if(!results){ |
---|
45 | results = new Array(); |
---|
46 | } |
---|
47 | |
---|
48 | // add new value |
---|
49 | results.push(value); |
---|
50 | |
---|
51 | dojox.storage.put("myValues", results, function(status, keyName){ |
---|
52 | if(status == dojox.storage.FAILED){ |
---|
53 | alert("You do not have permission to store data for this web site."); |
---|
54 | }else if(status == dojox.storage.SUCCESS){ |
---|
55 | loadValues(); |
---|
56 | } |
---|
57 | }); |
---|
58 | } |
---|
59 | |
---|
60 | // wait until the storage system is finished loading |
---|
61 | if(!dojox.storage.manager.isInitialized()){ |
---|
62 | dojo.connect(dojox.storage.manager, "loaded", runDemo); |
---|
63 | }else{ |
---|
64 | dojo.connect(dojo, "loaded", runDemo); |
---|
65 | } |
---|
66 | </script> |
---|
67 | </head> |
---|
68 | |
---|
69 | <body> |
---|
70 | <h1>Dojo Storage Hello World</h1> |
---|
71 | |
---|
72 | <p>Simple Dojo Storage example. Enter values below to have them |
---|
73 | persisted in Dojo Storage; refresh browser page or close browser |
---|
74 | and then return to this page to see the values again. Note that |
---|
75 | Dojo Storage will not work from file:// URLs.</p> |
---|
76 | |
---|
77 | <h2>Save Values:</h2> |
---|
78 | <div> |
---|
79 | <input id="saveValue" type="text"></input> |
---|
80 | <button id="saveButton">Save Value</button> |
---|
81 | </div> |
---|
82 | |
---|
83 | <h2>All Saved Values:</h2> |
---|
84 | <p id="allValues"></p> |
---|
85 | |
---|
86 | <p>Using Dojo Storage Provider (autodetected): |
---|
87 | <span id="currentProvider"></span> |
---|
88 | <p> |
---|
89 | </body> |
---|
90 | </html> |
---|