Table of Contents

ChainableStore

Inherits:Store

Overview

The ChainableStore is a class that inherits the Store class. The ChainableStore can be chained to another ChainableStore object. When chained, it tries to retrieve the item from the chained Store object if the requested item 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

The object that contains a copy of all items in both stores. The copy is created using clone() method. The stored items in its own store are initialized to the passed values when set.


localItems

Type:Object get

The object that contains items in its own store. It doesn't contain items in the chained store.

Constructor

Constructor(options)

Parameters

ParameterTypeDescription
optionsObjectThe options to initialize the Store. Has the following keys in addition to the keys available to the parent class:
“chain”StoreThe store object to chain.
“writeThrough”BooleanIf true, then it modifies the chained store when set()/merge() instead of itself.

Methods

chain(store)

Type:Undefined

Chains to another ChainableStore object. Only one ChainableStore can be chained. If you call chain() several times, it is overwritten by the latter one.

Parameters

ParameterTypeDescription
store
Required
ChainableStoreA ChainableStore object to chain.

Return Value

Undefined.


clone()

Type:Object

Returns the deep copy of stored items. When the store is chained to another store, it clones both the locally stored items and chained items and then merges them using BitmistJS's deep merge utility function.

Parameters

None.

Return Value

The deep copy of items in both stores.


get(key, defaultValue)

Type:*

Returns the value specified by the key parameter.

It tries to retrieve the item from the chained store object if the item requested does not exist in its own store. If the item exists in both store and mergeable, both items are cloned and merged using BitmistJS's deep merge utility function.

If the item doesn't exist in both stores, the default value is returned.

Parameters

ParameterTypeDescription
key
Required
StringThe key to be retrieved.
defaultValue*The default value to be returned when the key doesn't exist.

Return Value

The value specified by the key. When the key is not found, returns defaultValue, or undefined if defaultValue is missing.


has(key)

Type:Boolean

Returns if the specified key exists.

It tries to retrieve the item from the chained store object if the item requested does not exist in its own store.

Parameters

ParameterTypeDescription
key
Required
StringThe key to check.

Return Value

True if the key exists, false if it doesn't exist.


merge(value, merger, options)

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. If “writeThrough” option is true in either the constructor option or the option parameter, it merges with items in the chained store.

Parameters

ParameterTypeDescription
value
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 in order.
mergerFunctionThe function for merging objects. This parameter has a higher priority than merger property.
optionsObjectThe options. Has the following keys:
“writeThrough”BooleanIf True, then it sets the value to the chained store.

Return Value

Undefined.


set(key, value, options)

Type:Undefined

Sets the value to an item specified by the key parameter. If the key does not exist, it will be automatically created.

If the store is chained to another store, by default it sets the item to its own store. If “writeThrough” option is true in either the constructor option or the option parameter, it sets the item to the chained store.

Parameters

ParameterTypeDescription
key
Required
StringThe key to set the value.
value
Required
*The value to set.
optionsObjectThe options. Has the following keys:
“writeThrough”BooleanIf True, then it sets the value to te chained store.

Return Value

Undefined.