Upgrade to ExtJS 4.0.7 - Released 10/19/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="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../resources/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"><span id='Ext-state-LocalStorageProvider'>/**
19 </span> * @class Ext.state.LocalStorageProvider
20  * @extends Ext.state.Provider
21  * A Provider implementation which saves and retrieves state via the HTML5 localStorage object.
22  * If the browser does not support local storage, an exception will be thrown upon instantiating
23  * this class.
24  */
25
26 Ext.define('Ext.state.LocalStorageProvider', {
27     /* Begin Definitions */
28     
29     extend: 'Ext.state.Provider',
30     
31     alias: 'state.localstorage',
32     
33     /* End Definitions */
34    
35     constructor: function(){
36         var me = this;
37         me.callParent(arguments);
38         me.store = me.getStorageObject();
39         me.state = me.readLocalStorage();
40     },
41     
42     readLocalStorage: function(){
43         var store = this.store,
44             i = 0,
45             len = store.length,
46             prefix = this.prefix,
47             prefixLen = prefix.length,
48             data = {},
49             key;
50             
51         for (; i &lt; len; ++i) {
52             key = store.key(i);
53             if (key.substring(0, prefixLen) == prefix) {
54                 data[key.substr(prefixLen)] = this.decodeValue(store.getItem(key));
55             }            
56         }
57         return data;
58     },
59     
60     set : function(name, value){
61         var me = this;
62         
63         me.clear(name);
64         if (typeof value == &quot;undefined&quot; || value === null) {
65             return;
66         }
67         me.store.setItem(me.prefix + name, me.encodeValue(value));
68         me.callParent(arguments);
69     },
70
71     // private
72     clear : function(name){
73         this.store.removeItem(this.prefix + name);
74         this.callParent(arguments);
75     },
76     
77     getStorageObject: function(){
78         try {
79             var supports = 'localStorage' in window &amp;&amp; window['localStorage'] !== null;
80             if (supports) {
81                 return window.localStorage;
82             }
83         } catch (e) {
84             return false;
85         }
86         //&lt;debug&gt;
87         Ext.Error.raise('LocalStorage is not supported by the current browser');
88         //&lt;/debug&gt;
89     }    
90 });
91 </pre>
92 </body>
93 </html>