Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Collection

Darko Kukovec edited this page Jul 1, 2017 · 4 revisions

constructor

constructor(data?: Array<object>);

The collection constructor can receive an previously serialised collection store

length

The length property contains a total number of models in the collection instance.

snapshot

A current plain JS value of the collection. The value is always immutable - a new instance is created on every change. The property is a computed property, so it can be observed using MobX.

static types

The static types property should be an array of all custom model classes. More info in the "Configuring the collection" guide.

insert

public insert(data: Array<object>|object): Array<IModel>

Function used to insert serialised data to the store. The data should be serialised using either the serialised property or the toJS function on either a model or collection. The return value is an array of inserted models.

Important: add should be used for adding new or existing models to the collection.

add

add<T>(model: Array<IModel>): Array<T>
add<T>(model: IModel): T
add<T>(model: Array<any>, type?: IType): Array<T>
add<T>(model: object, type?: IType): T

The add method can receive an model instance (or an array of instances) or a plain JS object (or an array of them) and the model type. More details in the "Using the collection" guide.

find

find<T>(type: IType, id?: string | number): T

The find method will return the exact model (if the id parameter is provided), or the first model that was found (if the id parameter is not provided). If no models match, the value null will be returned.

findAll

findAll<T>(type: IType): Array<T>

The findAll method will return all models of the given type.

remove

remove<T>(type: IType, id?: string | number): T

The remove method will remove the exact model (if the id parameter is provided), or the first model that was found (if the id parameter is not provided) from the store. The removed model instance will be returned by the function

removeAll

removeAll<T>(type: IType): Array<T>

The removeAll method will remode all models of the given type from the collection, and it will return the list of removed model instances.

reset

reset(): void

The reset method will remove all models from the collection.

toJS

toJS(): Array<IDictionary>

The toJS method will return the serialised version of the collection. Since this is a plain JS object (actually, an array of objects), it can be transformed to a string and sent trough network or saved to localStorage and be reinitialised later.

patchListen

patchListen(listener: (data: IPatch, model: IModel) => void): () => void

Add a listener for changes on the collection. The listener function will be called with an IPatch object

applyPatch

applyPatch(patch: IPatch): void;

Apply an an IPatch on the collection.