Bitsmist Frameworks
Docs » ChainableStore

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

ParameterTypeDescription
settingsObjectAn object to initialize the Store. In addition to the keys available to the parent class, new keys are:
“chain”StoreA store object to chain.
“writeThrough”BooleanIf 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

ParameterTypeDescription
store
Required
ChainableStoreA 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:

  1. Local value
  2. Chained value
  3. Default value

Returns undefined if none exists.

Parameters

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

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

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

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

  • ChainableStore
    • Overview
    • Properties
      • items
      • localItems
    • Constructor
      • Constructor(settings)
    • Methods
      • chain(store)
      • clone()
      • get(key, defaultValue)
      • merge(newItems, merger)
      • 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