v7.3.0

State
Mixin

A mixin that handles accessing, saving, and restoring an object's persistent state.

Using Stateful Components

Instances of classes that use this mixin (i.e., "stateful components") have a state property that provides read/write access to their persistable state in the form of a simple object. These state objects can be saved and restored under application control, e.g., using localStorage.

This approach can be streamlined using a StateProvider either by setting the default state provider or by using an instance-level stateProvider config.

When using a state provider, stateful components with a stateId or an id will automatically save (see saveState) and restore (see loadState) their state. This use of the id as a stateId can be disabled by assigning the stateful config to false. When using a stateId and a state provider, it is not necessary to call the loadState and saveState methods directly.

Simple vs Complex State

Some stateful components (e.g., panels) have state that can be described purely by their config properties. For these components, the stateful config can be used to control which config properties to include in their persistent state. For example:

 const mainPanel = new Panel({
     collapsible : true,
     stateId     : 'mainPanel',
     stateful    : ['collapsed']
 });

Other components have a complex state (e.g., GridState), and do not use the stateful config in this way. In all other ways, however, these components behave the same as their simple state counterparts.

Alter state information

For complex state components, the beforeStateSave event can be used to alter the state object:

const grid = new Grid({
   stateId   : 'grid',
   listeners : {
      beforeStateSave : ({ state }) => {
         // Removes state info for the store, like sorters and filters
         delete state.store;
      }
   }
}

Implementing Stateful Components

Implementors of stateful components have two main design points to consider:

  • Getting and setting their persistent state object.
  • Initiating calls to saveState when the object's persistent state changes.

Persistent State

For simple cases, the stateful config can be set to the list of config property names that should be saved:

 class MyStatefulComponent extends Base.mixin(State) {
     static configurable = {
         stateful : ['text', 'size']
     };
 }

While the stateful config supports an object form (where keys with truthy values are the config names), this form is typically reserved for configuring instances.

Classes can choose to implement the getState and applyState methods to enhance the state object with data not easily mapped to config properties. These method can call their super methods or fully replace them.

 class MyStatefulComponent extends Base.mixin(State) {
     getState() {
         return {
             text : this.text,
             size : this.size
         };
     }

applyState(state) { this.text = state.text; this.size = state.size; } }

Saving Dates

A stateful property may be a Date property if the changeDate method of the class accepts an ISO 8601 formatted date. Dates are saved in state using ISO 8601 format: YYYY-MM-DDTHH:mm:ssZ

Saving State

When the persistent state of a stateful component changes, it must call saveState. This method schedules an update of the component's persistence state with the appropriate stateProvider. When a config property named in the stateful config changes, this call will be made automatically. This means that even if a component replaces getState and applyState, it can still be helpful to specify a value for the stateful config.

 class MyStatefulComponent extends Base.mixin(State) {
     static configurable = {
         stateful : ['text', 'size']
     };

getState() { ... } applyState(state) { ... } }

Another way to ensure saveState is called when necessary is to use statefulEvents.

 class MyStatefulComponent extends Base.mixin(State) {
     static configurable = {
         statefulEvents : ['change', 'resize']
     };
 }

Useful configs and functions

Config / Function Description
stateId Unique key used to save and load state
stateful List of config properties to persist, or false
stateProvider Provider instance for automatic save/load
saveState Persist the current state
loadState Restore previously saved state

See also

No results

Configs

Configs are options you supply in a configuration object when creating an instance of this class

Properties

Properties are getters/setters or publicly accessible variables on this class
  • isState : Booleantrue
    READONLY
    static
    ADVANCED
    Identifies an object as an instance of State class, or subclass thereof.
  • isStateful : Boolean
    READONLY
    ADVANCED

    Returns true if this instance implements the State interface.

  • isStatefulActive : Boolean
    internal
    READONLY

    Returns true if this instance is ready to participate in state activities.

  • isState : Booleantrue
    READONLY
    ADVANCED
    Identifies an object as an instance of State class, or subclass thereof.
  • Gets or sets a component's state

  • statefulId : String
    internal

    Returns the state key to use for this instance. This will be either the stateId or the id (if explicitly specified and stateful is not false).

  • statefulness : Object
    private
    READONLY

    Returns an object whose truthy keys are the config properties to include in this object's state.

Functions

Functions are methods available for calling on the class

    Events

    Events are triggered for certain actions in this class and can be listened for to react to those actions in your code

    Event handlers

    Event handlers are callbacks called as a result of certain actions in this class

    Source path

    Core/mixin/State.js

    Contents