ChainableStore
Inherit:Store
Overview
The ChainableStore is a class derived from the Store class. The ChainableStore can be chained to another ChainableStore object. When chained, it tries to retrieve an item from the chained Store object if the item requested does not exist in its own store.
On this page, only the difference from the parent class is explained.
Properties
items
Type:Object
get/set
Returns the object that contains all items in the Store. It also contains items in a chained Store object if chained.
When set it stores items in its own store. It doesn't affect the chained store.
localItems
Type:Object
get
Returns the object that contains items in its own Store. It doesn't contain items in a chained Store.
Constructor
Constructor(settings)
Parameters
Parameter | Type | Description |
---|---|---|
settings | Object | An object to initialize the Store. In addition to the keys available to the parent class, new keys are: |
“chain” | Store | A store object to chain. |
“writeThrough” | Boolean | If True, then it modifies a chained store when set()/merge(). |
Methods
chain(store)
Type:undefined
Chains to another ChainableStore object. When a ChainableStore is chained, if a nonexistent item is requested by get() method it tries to retrieve the item from the chained store. Only one ChainableStore can be chained. If you call chain() several times, it is overwritten by the latter one, thus only the latest one is valid.
Parameters
Parameter | Type | Description |
---|---|---|
store Required | ChainableStore | A ChainableStore object to chain. |
Return Value
undefined
clone()
Type:Object
Returns a deep copy of items. When the store is chained to another store it contains all the items in the chained store.
Parameters
none
Return Value
A copy of items in the store.
get(key, defaultValue)
Type:*
Returns a value specified by the key parameter. Returns a default value if the key doesn't exist. If chained to another store, then the item will be searched in order:
- Local value
- Chained value
- Default value
Returns undefined if none exists.
Parameters
Parameter | Type | Description |
---|---|---|
key Required | String | A key of an item. |
defaultValue | * | A default value returned when the key doesn't exist. |
Return Value
A value specified by the key.
merge(newItems, merger)
Type:undefined
Merges new items into current items. If the store is chained to another store, by default it merges with its own store items, however “writeThrough” option is set to True in the constructor's option then it merges with items in the chained store.
Parameters
Parameter | Type | Description |
---|---|---|
newItems Required | Object/ Array of objects | An object to merge or an array of objects. If the newItems parameter is an array, all objects in the array will be merged. |
merger | Function | A function for merging objects. This parameter has a higher priority than merger property. |
Return Value
undefined
set(key, value)
Type:undefined
Set a value to an item specified by the key parameter. If the key does not exist, it will be automatically created. If the item already exists in a store and it is an object, the value will be merged with the current item.
If the store is chained to another store, by default it sets an item in its own store, however “writeThrough” option is set to True in the constructor's option then it sets an item in the chained store.
You can use a period-separated string to specify a hierarchy. The below example sets the value to store[“settings”][“name”].
store.set("settings.name", "BarHeader");
If the item does not exist, then it will be created automatically. In the above example, if “settings” does not exist, the “settings” object will be created automatically.
Parameters
Parameter | Type | Description |
---|---|---|
key Required | String | Key of the item. |
value Required | * | A value to set. |
Return Value
Undefined