Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / LocalStorageProvider.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js">Ext.define('Ext.state.LocalStorageProvider', {
19     /* Begin Definitions */
20     
21     extend: 'Ext.state.Provider',
22     
23     alias: 'state.localstorage',
24     
25     /* End Definitions */
26    
27     constructor: function(){
28         var me = this;
29         me.callParent(arguments);
30         me.store = me.getStorageObject();
31         me.state = me.readLocalStorage();
32     },
33     
34     readLocalStorage: function(){
35         var store = this.store,
36             i = 0,
37             len = store.length,
38             prefix = this.prefix,
39             prefixLen = prefix.length,
40             data = {},
41             key;
42             
43         for (; i &lt; len; ++i) {
44             key = store.key(i);
45             if (key.substring(0, prefixLen) == prefix) {
46                 data[key.substr(prefixLen)] = this.decodeValue(store.getItem(key));
47             }            
48         }
49         return data;
50     },
51     
52     set : function(name, value){
53         var me = this;
54         
55         me.clear(name);
56         if (typeof value == &quot;undefined&quot; || value === null) {
57             return;
58         }
59         me.store.setItem(me.prefix + name, me.encodeValue(value));
60         me.callParent(arguments);
61     },
62
63     // private
64     clear : function(name){
65         this.store.removeItem(this.prefix + name);
66         this.callParent(arguments);
67     },
68     
69     getStorageObject: function(){
70         try {
71             var supports = 'localStorage' in window &amp;&amp; window['localStorage'] !== null;
72             if (supports) {
73                 return window.localStorage;
74             }
75         } catch (e) {
76             return false;
77         }
78         //&lt;debug&gt;
79         Ext.Error.raise('LocalStorage is not supported by the current browser');
80         //&lt;/debug&gt;
81     }    
82 });
83 </pre>
84 </body>
85 </html>