-
Notifications
You must be signed in to change notification settings - Fork 4
Store
constructor(data?: Array<object>);
The collection constructor can receive an previously serialised store
The length
property contains a total number of models in the store instance.
A current plain JS value of the store. 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.
The static types
property should be an array of all custom model classes. More info in the "Configuring the store" guide.
Defines if the fetch
and fetchAll
methods should cache their results. The default value is true
.
sync(body: JsonApi.IResponse): IModel|Array<IModel>
A method that sync the API response to the store - it creates the models and relationships. If the relationships aren't defined on custom records, it will create dynamic references. The returned value is a record or an array of records that were in the data
section of the JSON API response.
fetch(type: string, id: number|string, force?: boolean, options?: IRequestOptions): Promise<Response>
A method used to fetch a specific record. The method requires a record type and id. Optional parameters are:
-
force
- should we force a network request instead of potentially using an already cached response -
options
- an request object containing custom headers, options for pagination, sorting, included models or sparse fields
The return value is a promise that will resolve to a Response
object.
fetchAll(type: string, force?: boolean, options?: IRequestOptions): Promise<Response>
A method used to fetch records of a specific type. The method requires a record type. Optional parameters are:
-
force
- should we force a network request instead of potentially using an already cached response -
options
- an request object containing custom headers, options for pagination, sorting, included models or sparse fields
The return value is a promise that will resolve to a Response
object.
request(url: string, method: string = 'GET', data?: object, options?: IRequestOptions): Promise<Response>
A method for doing API calls that pass trough the whole library network layer an in the end resolve to a Response
object.
destroy(type: string, id: number|string, options?: IRequestOptions): Promise<boolean>
A method for removing a record from the store and API. It will resolve to true if removal was successful or reject with an error.
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 record or store. The return value is an array of inserted records.
Important: add
should be used for adding new or existing records to the store.
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 store" guide.
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<T>(type: IType): Array<T>
The findAll
method will return all models of the given type.
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<T>(type: IType): Array<T>
The removeAll
method will remode all models of the given type from the store, and it will return the list of removed model instances.
reset(): void
The reset
method will remove all models from the store.
toJS(): Array<IDictionary>
The toJS
method will return the serialised version of the store. 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(listener: (data: IPatch, model: IModel) => void): () => void
Add a listener for changes on the store. The listener function will be called with an IPatch object
applyPatch(patch: IPatch): void;
Apply an an IPatch on the store.