StateProvider
Instances of this class are used to manage data storage for objects that use the State mixin, i.e. stateful components. When such components change their stateful properties, they notify the associated stateProvider, which will save the changes after a short delay (to allow multiple changes to coalesce into a single save operation).
There are two (2) built-in types of storage supported by StateProvider:
local: Stores data in the browser'slocalStorage. Because of this, allStateProviderinstances share their state data if they have the same prefix.memory: Stores data in the provider's memory. Each instance has its own storage. This is typically used when the state data is saved to a backend server.
Using local Storage
The global StateProvider is typically to use localStorage for the page or application like so:
StateProvider.setup('local');
With this provider in place, all stateful components will save their state to this provider by default.
This is the most typical, and recommended, strategy for proving data to stateful components. This approach allows various widgets on the page to simply declare their stateId to participate in the saving and restoring of application state.
Because this storage type uses localStorage, the StateProvider applies a string prefix to isolate its data from other users of localStorage. The default prefix is 'bryntum-state:', but this can be configured to a different value. This could be desired, for example, to isolate state data from multiple pages or for version changes.
StateProvider.setup({
storage : 'local',
prefix : 'myApp-v1:'
});
Using memory Storage
In some applications it may be desirable to save state to a server and restore it on other devices for the user. Because state data is consumed synchronously, and server I/O is asynchronous, the StateProvider can be configured to use 'memory' storage and the actual state data can be loaded/saved by the application.
Two factors are important to consider before deciding to save application state on the server (beyond the async adaptation):
- State properties are often more of a reflection of the user's device than they are application preferences and, therefore, may not apply well on other devices.
- Potentially undesired application state will not be cleared by clearing local browser user data (a common troubleshooting strategy) and will follow the user to other browsers (another common troubleshooting technique).
The use this type of storage, the global StateProvider is configured like so:
StateProvider.setup('memory');
In this scenario, application code would download the user's state and use data to populate the StateProvider.instance. In this case, the save event is used to save the data back to the server when it changes.
See state demo for a usage example.
Useful configs, properties and functions
| Config / Property / Function | Description |
|---|---|
| setup | Create and set the global state provider |
| instance | The global default state provider |
| storage | Storage type: 'local' or 'memory' |
| prefix | Key prefix for localStorage isolation |
| data | Read/write all stored state data |
See also
- State - Mixin for stateful components
Configs
Configs are options you supply in a configuration object when creating an instance of this class-
The key prefix applied when using the
'local'storage type.
Properties
Properties are getters/setters or publicly accessible variables on this class-
The default stateProvider for stateful objects.
-
On read, this property returns all state data stored in the provider. On write, this property adds all the given values to the state provider's data. To replace the data, call clear before assigning this property. This is used to bulk populate this
StateProviderwith data for stateful components. -
Storage instance
Has a corresponding storage config.
Functions
Functions are methods available for calling on the class-
writeStatefuls( )private
A delayable method that flushes pending stateful objects.