StateMap

StateMap

Class to add events dispatching to the JS Map class.

When the state has a list of objects (with IDs) it will be converted into a StateMap. StateMap is used almost in the same way as a regular JS map. Because all elements have an id attribute, it has some specific methods:

  • add: a convenient method to add an element without specifying the key ("id" attribute will be used as a key).
  • loadValues: to add many elements at once wihout specifying keys ("id" attribute will be used).

Apart, the main difference between regular Map and MapState is that this one will inform any change to the state manager.

Constructor

new StateMap(name, stateManager, iterable)

Source:

Create a reactive Map.

Parameters:
Name Type Description
name string

the property name

stateManager StateManager

the state manager

iterable iterable

an iterable object to create the Map

Methods

add(value) → {Map}

Source:

Insert a new element int a list.

Each value needs it's own id attribute. Objects withouts id will be rejected.

Parameters:
Name Type Description
value object

the value to add (needs an id attribute)

Returns:

the resulting Map object

Type
Map

checkValue(value)

Source:

Check if a value is valid to be stored in a a State List.

Only objects with id attribute can be stored in State lists.

This method throws an error if the value is not valid.

Parameters:
Name Type Description
value object

(with ID)

delete(key) → {boolean}

Source:

Delete an element from the map.

Parameters:
Name Type Description
key *
Returns:
Type
boolean

get(key) → {Object}

Source:

Return a state map element.

Parameters:
Name Type Description
key *

the element id

Returns:
Type
Object

has(key) → {boolean}

Source:

Check whether an element with the specified key exists or not.

Parameters:
Name Type Description
key *

the key to find

Returns:
Type
boolean

loadValues(values) → {StateMap}

Source:

Insert a full list of values using the id attributes as keys.

This method is used mainly to initialize the list. Note each element is indexed by its "id" attribute. This is a basic restriction of StateMap. All elements need an id attribute, otherwise it won't be saved.

Parameters:
Name Type Description
values iterable

the values to load

Returns:

return the this value

Type
StateMap

normalizeKey(key) → {string}

Source:

Return a normalized key value for state map.

Regular maps uses strict key comparissons but state maps are indexed by ID.JSON conversions and webservices sometimes do unexpected types conversions so we convert any integer key to string.

Parameters:
Name Type Description
key *

the provided key

Returns:
Type
string

set(key, value) → {Map}

Source:

Set an element into the map.

Each value needs it's own id attribute. Objects without id will be rejected. The function will throw an error if the value id and the key are not the same.

Parameters:
Name Type Description
key *

the key to store

value *

the value to store

Returns:

the resulting Map object

Type
Map

toJSON() → {array}

Source:

Return a suitable structure for JSON conversion.

This function is needed because new values are compared in JSON. StateMap has Private attributes which cannot be stringified (like this.stateManager which will produce an infinite recursivity).

Returns:
Type
array