Store
Overview
The Store class is a utility class for storing information such as settings. The Store can store hierarchical data.
Properties
items
Type:Object
get/set
Returns the object that contains a copy of all items in the Store. The stored Items are reset to the passed values when set.
merger
Type:Function
get/set
Returns the merge function to be used in the merge() method. By default, the internal deep merge function is used. Both merge source object and target object are passed to this merge function. Return the merged object from the function.
merger(targetObject, sourceObject); // Merge sourceObject into targetObject.
Constructor
Constructor(settings)
Parameters
Parameter | Type | Description |
---|---|---|
settings | Object | An object to initialize the Store. Keys are: |
“items” | Object | An object with items to initialize a Store. Same as setting items property. |
“merger” | Function | A merge function object. Same as setting merger property. |
Methods
clear()
Type:undefined
Removes all items from the Store.
Parameters
none
Return Value
undefined
clone()
Type:Object
Returns a deep copy of items.
Parameters
none
Return Value
A copy of items in the store.
get(key, defaultValue)
Type:*
Returns the value specified by the key parameter. Returns the default value if the key doesn't exist.
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");
Parameters
Parameter | Type | Description |
---|---|---|
key Required | String | A key of a stored item. |
defaultValue | * | A default value returned when the key doesn't exist. |
Return Value
A value specified by the key. When the item is not found, returns defaultValue or undefined if defaultValue is missing.
has(key)
Type:Boolean
Returns if the specified key exists.
Parameters
Parameter | Type | Description |
---|---|---|
key Required | String | A key to check existence. |
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
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
remove(key)
Type:undefined
Removes an item that has the specified key.
Parameters
Parameter | Type | Description |
---|---|---|
key Required | String | A key for an item to remove. |
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 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, a hierarchy called “settings” is automatically created. However, if the key 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 will be an error.
Parameters
Parameter | Type | Description |
---|---|---|
key Required | String | Key of the item. |
value Required | * | A value to set. |
Return Value
Undefined