1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-state.Provider'>/**
2 </span> * @class Ext.state.Provider
3 * <p>Abstract base class for state provider implementations. The provider is responsible
4 * for setting values and extracting values to/from the underlying storage source. The
5 * storage source can vary and the details should be implemented in a subclass. For example
6 * a provider could use a server side database or the browser localstorage where supported.</p>
8 * <p>This class provides methods for encoding and decoding <b>typed</b> variables including
9 * dates and defines the Provider interface. By default these methods put the value and the
10 * type information into a delimited string that can be stored. These should be overridden in
11 * a subclass if you want to change the format of the encoded value and subsequent decoding.</p>
13 Ext.define('Ext.state.Provider', {
15 observable: 'Ext.util.Observable'
18 <span id='Ext-state.Provider-cfg-prefix'> /**
19 </span> * @cfg {String} prefix A string to prefix to items stored in the underlying state store.
20 * Defaults to <tt>'ext-'</tt>
24 constructor : function(config){
25 config = config || {};
27 Ext.apply(me, config);
28 <span id='Ext-state.Provider-event-statechange'> /**
29 </span> * @event statechange
30 * Fires when a state change occurs.
31 * @param {Provider} this This state provider
32 * @param {String} key The state key which was changed
33 * @param {String} value The encoded value for the state
35 me.addEvents("statechange");
37 me.mixins.observable.constructor.call(me);
40 <span id='Ext-state.Provider-method-get'> /**
41 </span> * Returns the current value for a key
42 * @param {String} name The key name
43 * @param {Mixed} defaultValue A default value to return if the key's value is not found
44 * @return {Mixed} The state data
46 get : function(name, defaultValue){
47 return typeof this.state[name] == "undefined" ?
48 defaultValue : this.state[name];
51 <span id='Ext-state.Provider-method-clear'> /**
52 </span> * Clears a value from the state
53 * @param {String} name The key name
55 clear : function(name){
57 delete me.state[name];
58 me.fireEvent("statechange", me, name, null);
61 <span id='Ext-state.Provider-method-set'> /**
62 </span> * Sets the value for a key
63 * @param {String} name The key name
64 * @param {Mixed} value The value to set
66 set : function(name, value){
68 me.state[name] = value;
69 me.fireEvent("statechange", me, name, value);
72 <span id='Ext-state.Provider-method-decodeValue'> /**
73 </span> * Decodes a string previously encoded with {@link #encodeValue}.
74 * @param {String} value The value to decode
75 * @return {Mixed} The decoded value
77 decodeValue : function(value){
88 re = /^(a|n|d|b|s|o|e)\:(.*)$/,
89 matches = re.exec(unescape(value)),
95 if(!matches || !matches[1]){
105 return parseFloat(value);
107 return new Date(Date.parse(value));
109 return (value == '1');
113 Ext.each(value.split('^'), function(val){
114 all.push(me.decodeValue(val));
121 Ext.each(value.split('^'), function(val){
122 keyValue = val.split('=');
123 all[keyValue[0]] = me.decodeValue(keyValue[1]);
132 <span id='Ext-state.Provider-method-encodeValue'> /**
133 </span> * Encodes a value including type information. Decode with {@link #decodeValue}.
134 * @param {Mixed} value The value to encode
135 * @return {String} The encoded value
137 encodeValue : function(value){
146 } else if(typeof value == 'number') {
148 } else if(typeof value == 'boolean') {
149 enc = 'b:' + (value ? '1' : '0');
150 } else if(Ext.isDate(value)) {
151 enc = 'd:' + value.toGMTString();
152 } else if(Ext.isArray(value)) {
153 for (len = value.length; i < len; i++) {
154 flat += this.encodeValue(value[i]);
160 } else if (typeof value == 'object') {
162 if (typeof value[key] != 'function' && value[key] !== undefined) {
163 flat += key + '=' + this.encodeValue(value[key]) + '^';
166 enc = 'o:' + flat.substring(0, flat.length-1);
172 });</pre></pre></body></html>