Collection
The JSON API Collection enhances the datx
collection with the following properties and methods:
defaultModel
static defaultModel: typeof PureModel
A default model that will be used if no model is defined for a certain model type received from the server (default is GenericModel
which is a combination of JSON API Model and PureModel
).
sync
sync<T extends IJsonapiModel = IJsonapiModel>(body?: IResponse): T|Array<T>|null;
Add the data from a JSON API response to the store. The return value is a model or an array of models from the JSON API data
property.
getOne
getOne<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, id: string, options?: IRequestOptions): Promise<Response<T>>
Get a single model from the server.
The options can be used to send additional parameters to the server.
If an error happens, the function will reject with the Response
object with the error
property set.
getMany
getMany<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, options?: IRequestOptions)
Fetch multiple models of the given type from the server. This will either be all models or a first page of models, depending on the server configuration.
The options can be used to send additional parameters to the server.
If an error happens, the function will reject with the Response
object with the error
property set.
getAll
getAll<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, options?: IRequestOptions, maxRequests?: number = 50): Promise<IGetAllResponse<T>>
Fetches all records within the request limit of the given type from the server. Generally you don't want to use this function if you are trying to load a lot of pages at once since it could lead to the huge response time.
Unlike other collection methods that return a Response
instance, getAll
returns an object that contains:
- data: array of all models
- responses: array of all responses that came from the server
- lastResponse:
Response
instance for easy acces to allResponse
properties
See IGetAllResponse
interface for detailed info.
The options can be used to send additional parameters to the server.
If an error happens, the function will reject with the Response
object with the error
property set.
request
request<T extends IJsonapiModel = IJsonapiModel>(url: string, method?: string, data?: object, options?: IRequestOptions): Promise<Response<T>>
Make a custom API request to the server. The response will still be parsed in the same way it would be in getOne
or getMany
.
removeOne
removeOne(type: IType|typeof PureModel, id?: IIdentifier, remote?: boolean|IRequestOptions): Promise<void>;
removeOne(model: PureModel, remote?: boolean|IRequestOptions): Promise<void>;
The removeOne
method equal to the one in the datx
collection, but with an additional optional remote
property. If set, the model will also be removed from the server.
removeAll and reset
The removeAll
and reset
methods are equal to the datx
collection methods, but will also clear the network cache.