Table of Contents

Store

Overview

The Store class is a utility class for storing information such as settings. The Store can store hierarchical data.

Properties

options

Type:Object get/set

The store options.


items

Type:Object get/set

The object that contains a copy of all items in the store. The copy is created using clone() method.


merger

Type:Function get/set

The merge function to be used in the merge() method. By default, BitmistJS's deep merge utility function is used.

Both the merge source object and target object are passed to this merge function. Return the merged object from the merge function.

merger(targetObject, sourceObject); // merge sourceObject into targetObject

Constructor

Constructor(options)

Parameters

ParameterTypeDescription
optionsObjectThe object to initialize the Store. Has the following keys:
“items”ObjectThe items object to initialize a Store.
“merger”FunctionThe merge function object.

Methods

clear()

Type:Undefined

Removes all items from the Store.

Parameters

None.

Return Value

Undefined.


clone()

Type:Object

Returns the deep copy of items this store holds.

Parameters

None.

Return Value

The deep copy of stored items.


get(key, defaultValue)

Type:*

Returns the value specified by the key parameter. Returns the default value if the key doesn't exist. The value will be deep copied if the value is an array or object.

You can use a period-separated string to specify a hierarchy. The below example returns the value of store[“settings”][“name”] from the store.

store.get("settings.name");

No exception is thrown even if any of the intermediate levels of the hierarchy do not exist. In the above example, it returns Undefined if “settings” does not exist, without an error.

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.

Parameters

ParameterTypeDescription
key
Required
StringThe key to check.

Return Value

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


merge(newItems, merger)

Type:Undefined

Merges new items into current items.

Parameters

ParameterTypeDescription
newItems
Required
Object/
Array of objects
The object to merge or 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.

Return Value

Undefined.


remove(key)

Type:Undefined

Removes the item that has the specified key.

Parameters

ParameterTypeDescription
key
Required
StringThe key to remove.

Return Value

Undefined.


set(key, value)

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 item already exists in the store, the value will be overwritten by the new value.

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 intermediate hierarchies do not exist, they will be created automatically. In the above example, if “settings” does not exist, the hierarchy “settings” is automatically created. However, if the intermediate hierarchy already exists and is not an object, an error will be thrown. For example, if the above code is executed on a Store object with content {“settings”: “value”}, the “settings ” hierarchy cannot be created and it causes an error.

Parameters

ParameterTypeDescription
key
Required
StringThe key to set the value.
value
Required
*The value to set.

Return Value

Undefined.