Bitsmist Frameworks
Docs » Store

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

ParameterTypeDescription
settingsObjectAn object to initialize the Store. Keys are:
“items”ObjectAn object with items to initialize a Store. Same as setting items property.
“merger”FunctionA 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

ParameterTypeDescription
key
Required
StringA 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

ParameterTypeDescription
key
Required
StringA 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

ParameterTypeDescription
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.
mergerFunctionA 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

ParameterTypeDescription
key
Required
StringA 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

ParameterTypeDescription
key
Required
StringKey of the item.
value
Required
*A value to set.

Return Value

Undefined

Previous Next

© 2019-2023 Masaki Yasutake

Bitsmist Frameworks

Table of Contents

Table of Contents

  • Store
    • Overview
    • Properties
      • items
      • merger
    • Constructor
      • Constructor(settings)
    • Methods
      • clear()
      • clone()
      • get(key, defaultValue)
      • has(key)
      • merge(newItems, merger)
      • remove(key)
      • set(key, value)

GENERAL

  • Overview
  • Installation
  • Create a sample component

COMPONENT

  • Loading
  • Settings
  • Events
  • Extending with organizers

REFERENCE - COMPONENT

  • Component

REFERENCE - ORGANIZER

  • EventOrganizer
  • LoaderOrganizer
  • OrganizerOrganizer
  • SettingOrganizer
  • StateOrganizer
  • TemplateOrganizer

REFERENCE - STORE

  • Store
  • ChainableStore

REFERENCE - UTILITY

  • AjaxUtil
  • ClassUtil
  • Util