Methods

โ—๏ธinfo

Here are useful methods of the Collection Class listed.

setKey()#

Assigns a new Key/Name to our Collection.

MY_COLLECTION.setKey("newKey");
MY_COLLECTION.key; // Returns 'newKey'

โ“ Why a Key#

We recommended giving each Collection an unique Key. I promise you, it has only advantages.

  • helps us during debug sessions
  • makes it easier to identify a State
  • no need for separate persist Key

๐Ÿ“ญ Props#

PropTypeDefaultDescriptionRequired
valuestring | number | undefinedundefinedNew Key/Name of CollectionYes

๐Ÿ“„ Return#

Returns the Collection it was called on.




Group()#

Creates a new Group, without binding it properly to the Collection. This function is intended to be used in the Collection Config, because there the constructor will ensure that the Group gets bound to the Collection.

App.createCollection((collection) => ({
groups: {
myGroup: collection.Group(["item1", "item2"])
}
}))

For creating Groups in general we recommend using createGroup, because it binds the Group properly to the Collection, without further thinking.

๐Ÿ“ญ Props#

PropTypeDefaultDescriptionRequired
initialItemsArray<string | number>[]Initial ItemKeys of the GroupNo
configGroupConfig{}ConfigurationNo

๐Ÿ“„ Return#

Returns a fresh Group.




Selector()#

Creates a new Selector, without binding it properly to the Collection. This function is intended to be used in the Collection Config, because there the constructor will ensure that the Selector gets bound to the Collection.

App.createCollection((collection) => ({
selectors: {
mySelector: collection.Selector("item1")
}
}))

For creating Selectors in general we recommend using createSelector, because it binds the Selector properly to the Collection without further thinking.

๐Ÿ“ญ Props#

PropTypeDefaultDescriptionRequired
initialKeystring | numberundefinedKey of Item which the Selector representsNo
configSelectorConfig{}ConfigurationNo

๐Ÿ“„ Return#

Returns a fresh Selector.




initSelectors()#

๐Ÿ”ฅwarning

No public function! Is public because of testing hehe.. It creates the default Selector and binds the Selectors created in the Collection Config to the Collection.




initGroups()#

๐Ÿ”ฅwarning

No public function! Is public because of testing hehe.. It creates the default Group and binds the Groups created in the Collection Config to the Collection.




collect()#

Allows us to collect Data and add it to our Collection.

MY_COLLECTION.collect({id: 1, name: "jeff"}); // Collect one Data
MY_COLLECTION.collect([{id: 9, name: "hans"}, {id: 22, name: "frank"}]); // Collect multiple Datas

Each Data needs one primaryKey to properly be identified later. In the above example, the primaryKey property is 'id', but we can change it in the Collection Config. If one Data Object contains a primaryKey which already exists, the existing Data will be overwritten by default.

To quickly add Data to specific Groups, the collect method takes groupKeys beside the to collect Data. A Group is like an interface to the Data of a Collection. Each collected Data will be added to the 'default' Group by default.

MY_COLLECTION.collect({id: 1, name: "jeff"}, ["group1", "group2"]);

For each not existing passed groupKey, a new Group will automatically be created. For instance if the 'group1' from the above example doesn't exist, a Group with the initial itemKeys ('[1]'), and the key 'group1' gets created. This group can be returned later with for example getGroup.

๐Ÿ“ญ Props#

PropTypeDefaultDescriptionRequired
dataDataType | Array<DataType> (DataType = Object)[]Data which gets added to the CollectionNo
groupKeysArray<string | number>[]Keys of Groups to which the Data gets addedNo
configCollectConfig{}ConfigurationNo

๐Ÿ“„ Return#

Returns the Collection it was called on.




update()#

With this function we can update already collected Data.

MY_COLLECTION.collect({id: 1, name: "jeff"});
MY_COLLECTION.update(1, {name: "frank"});

Here the primary Key gets useful, which we have defined in the collect method before. As the first property update takes the primaryKey and as second property the Data which gets merged into the current Data of the Item. Be aware that the merge happens at the top level of the objects.

By default, new properties get added to the already collected Data, although they might not fit to the Interface ( Typescript) defined before. In case you don't want to add new properties to the Item, just set addNewProperties to false in the config object.

MY_COLLECTION.collect({id: 1, name: "jeff"});
MY_COLLECTION.update(1, {name: "hans", age: 12}, {addNewProperties: false}); // Item at '1' has value '{name: "hans"}'
MY_COLLECTION.update(1, {name: "frank", age: 10}); // Item at '1' has value '{name: "frank", age: 10}'

If you don't like the above described way of updating your Item Data, we can also collect the Data with an already existing primaryKey again, and it will overwrite the old one.

MY_COLLECTION.collect({id: 1, name: "jeff"});
MY_COLLECTION.collect({id: 2, name: "frank"});

๐Ÿ“ญ Props#

PropTypeDefaultDescriptionRequired
itemKeynumber | stringundefinedPrimary Key of Item which gets updatedYes
changesobject{}Data which gets merged into the current Item ValueYes
configUpdateConfig{}ConfigurationNo

๐Ÿ“„ Return#

Returns the Collection it was called on.




createGroup()#

Creates a new Group, with automatically binding it to the Collection.

MY_COLLECTION.createGroup('myNewGroup');
Last updated on