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.Stateful'>/**
2 </span> * @class Ext.state.Stateful
3 * A mixin for being able to save the state of an object to an underlying
4 * {@link Ext.state.Provider}.
6 Ext.define('Ext.state.Stateful', {
8 /* Begin Definitions */
11 observable: 'Ext.util.Observable'
14 requires: ['Ext.state.Manager'],
18 <span id='Ext-state.Stateful-cfg-stateful'> /**
19 </span> * @cfg {Boolean} stateful
20 * <p>A flag which causes the object to attempt to restore the state of
21 * internal properties from a saved state on startup. The object must have
22 * a <code>{@link #stateId}</code> for state to be managed.
23 * Auto-generated ids are not guaranteed to be stable across page loads and
24 * cannot be relied upon to save and restore the same state for a object.<p>
25 * <p>For state saving to work, the state manager's provider must have been
26 * set to an implementation of {@link Ext.state.Provider} which overrides the
27 * {@link Ext.state.Provider#set set} and {@link Ext.state.Provider#get get}
28 * methods to save and recall name/value pairs. A built-in implementation,
29 * {@link Ext.state.CookieProvider} is available.</p>
30 * <p>To set the state provider for the current page:</p>
31 * <pre><code>
32 Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
33 expires: new Date(new Date().getTime()+(1000*60*60*24*7)), //7 days from now
35 * </code></pre>
36 * <p>A stateful object attempts to save state when one of the events
37 * listed in the <code>{@link #stateEvents}</code> configuration fires.</p>
38 * <p>To save state, a stateful object first serializes its state by
39 * calling <b><code>{@link #getState}</code></b>. By default, this function does
40 * nothing. The developer must provide an implementation which returns an
41 * object hash which represents the restorable state of the object.</p>
42 * <p>The value yielded by getState is passed to {@link Ext.state.Manager#set}
43 * which uses the configured {@link Ext.state.Provider} to save the object
44 * keyed by the <code>{@link stateId}</code></p>.
45 * <p>During construction, a stateful object attempts to <i>restore</i>
46 * its state by calling {@link Ext.state.Manager#get} passing the
47 * <code>{@link #stateId}</code></p>
48 * <p>The resulting object is passed to <b><code>{@link #applyState}</code></b>.
49 * The default implementation of <code>{@link #applyState}</code> simply copies
50 * properties into the object, but a developer may override this to support
51 * more behaviour.</p>
52 * <p>You can perform extra processing on state save and restore by attaching
53 * handlers to the {@link #beforestaterestore}, {@link #staterestore},
54 * {@link #beforestatesave} and {@link #statesave} events.</p>
58 <span id='Ext-state.Stateful-cfg-stateId'> /**
59 </span> * @cfg {String} stateId
60 * The unique id for this object to use for state management purposes.
61 * <p>See {@link #stateful} for an explanation of saving and restoring state.</p>
64 <span id='Ext-state.Stateful-cfg-stateEvents'> /**
65 </span> * @cfg {Array} stateEvents
66 * <p>An array of events that, when fired, should trigger this object to
67 * save its state (defaults to none). <code>stateEvents</code> may be any type
68 * of event supported by this object, including browser or custom events
69 * (e.g., <tt>['click', 'customerchange']</tt>).</p>
70 * <p>See <code>{@link #stateful}</code> for an explanation of saving and
71 * restoring object state.</p>
74 <span id='Ext-state.Stateful-cfg-saveBuffer'> /**
75 </span> * @cfg {Number} saveBuffer A buffer to be applied if many state events are fired within
76 * a short period. Defaults to 100.
80 autoGenIdRe: /^((\w+-)|(ext-comp-))\d{4,}$/i,
82 constructor: function(config) {
85 config = config || {};
86 if (Ext.isDefined(config.stateful)) {
87 me.stateful = config.stateful;
89 if (Ext.isDefined(config.saveDelay)) {
90 me.saveDelay = config.saveDelay;
92 me.stateId = config.stateId;
94 if (!me.stateEvents) {
97 if (config.stateEvents) {
98 me.stateEvents.concat(config.stateEvents);
101 <span id='Ext-state.Stateful-event-beforestaterestore'> /**
102 </span> * @event beforestaterestore
103 * Fires before the state of the object is restored. Return false from an event handler to stop the restore.
104 * @param {Ext.state.Stateful} this
105 * @param {Object} state The hash of state values returned from the StateProvider. If this
106 * event is not vetoed, then the state object is passed to <b><tt>applyState</tt></b>. By default,
107 * that simply copies property values into this object. The method maybe overriden to
108 * provide custom state restoration.
110 'beforestaterestore',
112 <span id='Ext-state.Stateful-event-staterestore'> /**
113 </span> * @event staterestore
114 * Fires after the state of the object is restored.
115 * @param {Ext.state.Stateful} this
116 * @param {Object} state The hash of state values returned from the StateProvider. This is passed
117 * to <b><tt>applyState</tt></b>. By default, that simply copies property values into this
118 * object. The method maybe overriden to provide custom state restoration.
122 <span id='Ext-state.Stateful-event-beforestatesave'> /**
123 </span> * @event beforestatesave
124 * Fires before the state of the object is saved to the configured state provider. Return false to stop the save.
125 * @param {Ext.state.Stateful} this
126 * @param {Object} state The hash of state values. This is determined by calling
127 * <b><tt>getState()</tt></b> on the object. This method must be provided by the
128 * developer to return whetever representation of state is required, by default, Ext.state.Stateful
129 * has a null implementation.
133 <span id='Ext-state.Stateful-event-statesave'> /**
134 </span> * @event statesave
135 * Fires after the state of the object is saved to the configured state provider.
136 * @param {Ext.state.Stateful} this
137 * @param {Object} state The hash of state values. This is determined by calling
138 * <b><tt>getState()</tt></b> on the object. This method must be provided by the
139 * developer to return whetever representation of state is required, by default, Ext.state.Stateful
140 * has a null implementation.
144 me.mixins.observable.constructor.call(me);
145 if (me.stateful !== false) {
146 me.initStateEvents();
151 <span id='Ext-state.Stateful-method-initStateEvents'> /**
152 </span> * Initializes any state events for this object.
155 initStateEvents: function() {
156 this.addStateEvents(this.stateEvents);
159 <span id='Ext-state.Stateful-method-addStateEvents'> /**
160 </span> * Add events that will trigger the state to be saved.
161 * @param {String/Array} events The event name or an array of event names.
163 addStateEvents: function(events){
164 if (!Ext.isArray(events)) {
172 for (; i < len; ++i) {
173 me.on(events[i], me.onStateChange, me);
177 <span id='Ext-state.Stateful-method-onStateChange'> /**
178 </span> * This method is called when any of the {@link #stateEvents} are fired.
181 onStateChange: function(){
183 delay = me.saveDelay;
187 me.stateTask = Ext.create('Ext.util.DelayedTask', me.saveState, me);
189 me.stateTask.delay(me.saveDelay);
195 <span id='Ext-state.Stateful-method-saveState'> /**
196 </span> * Saves the state of the object to the persistence store.
199 saveState: function() {
204 if (me.stateful !== false) {
205 id = me.getStateId();
207 state = me.getState();
208 if (me.fireEvent('beforestatesave', me, state) !== false) {
209 Ext.state.Manager.set(id, state);
210 me.fireEvent('statesave', me, state);
216 <span id='Ext-state.Stateful-method-getState'> /**
217 </span> * Gets the current state of the object. By default this function returns null,
218 * it should be overridden in subclasses to implement methods for getting the state.
219 * @return {Object} The current state
221 getState: function(){
225 <span id='Ext-state.Stateful-method-applyState'> /**
226 </span> * Applies the state to the object. This should be overridden in subclasses to do
227 * more complex state operations. By default it applies the state properties onto
228 * the current object.
229 * @param {Object} state The state
231 applyState: function(state) {
233 Ext.apply(this, state);
237 <span id='Ext-state.Stateful-method-getStateId'> /**
238 </span> * Gets the state id for this object.
239 * @return {String} The state id, null if not found.
241 getStateId: function() {
246 id = me.autoGenIdRe.test(String(me.id)) ? null : me.id;
251 <span id='Ext-state.Stateful-method-initState'> /**
252 </span> * Initializes the state of the object upon construction.
255 initState: function(){
257 id = me.getStateId(),
260 if (me.stateful !== false) {
262 state = Ext.state.Manager.get(id);
264 state = Ext.apply({}, state);
265 if (me.fireEvent('beforestaterestore', me, state) !== false) {
266 me.applyState(state);
267 me.fireEvent('staterestore', me, state);
274 <span id='Ext-state.Stateful-method-destroy'> /**
275 </span> * Destroys this stateful object.
278 var task = this.stateTask;
282 this.clearListeners();
287 </pre></pre></body></html>