API
Table of Contents
- apiClient
- url
- isRequest
- rpc
- length
- map
- forEach
- url
- model
- toJS
- toArray
- slice
- peek
- isEmpty
- _ids
- at
- get
- filter
- find
- add
- reset
- remove
- set
- build
- create
- fetch
- toJS
- primaryKey
- urlRoot
- url
- isNew
- get
- has
- id
- changedAttributes
- changes
- hasChanges
- reset
- set
- fetch
- applyPatchChanges
- save
- destroy
apiClient
Sets or gets the api client instance
Parameters
-
adapter
Adapter -
options
{} (optional, default{}
)
Returns Adapter
url
Returns the resource's url.
Returns string
isRequest
Questions whether the request exists and matches a certain label
Parameters
-
label
string
Returns boolean
rpc
Call an RPC action for all those non-REST endpoints that you may have in your API.
Parameters
-
endpoint
string -
options
{}
Returns Request
length
Alias for models.length
Returns Number
map
Alias for models.map
Parameters
-
callback
function (model: Model): any
Returns Array<any>
forEach
Alias for models.forEach
Parameters
-
callback
function (model: Model): void
Returns void
url
Returns the URL where the model's resource would be located on the server.
Returns string
model
Specifies the model class for that collection
Parameters
-
attributes
{} (optional, default{}
)
toJS
Returns a JSON representation of the collection
Returns Array<{}>
toArray
Alias of slice
Returns Array<Model>
slice
Returns a defensive shallow array representation of the collection
Returns Array<Model>
peek
Returns a shallow array representation of the collection
Returns Array<Model>
isEmpty
Wether the collection is empty
Returns boolean
_ids
Gets the ids of all the items in the collection
Returns Array<Id>
at
Get a resource at a given position
Parameters
-
index
number
Returns Model?
get
Get a resource with the given id or uuid
Parameters
-
id
Id -
$1
any (optional, default{}
)-
$1.required
(optional, defaultfalse
)
-
Returns Model?
filter
Get resources matching criteria
Parameters
-
query
({} | function (Model): boolean)
Returns Array<Model>
find
Finds an element with the given matcher
Parameters
-
query
({} | function (Model): boolean) -
$1
any (optional, default{}
)-
$1.required
(optional, defaultfalse
)
-
Returns Model?
add
Adds a model or collection of models.
Parameters
-
data
(Array<({} | Model)> | {} | Model)
Returns void
reset
Resets the collection of models.
Parameters
-
data
Array<({} | Model)>
Returns void
remove
Removes the model with the given ids or uuids
Parameters
-
ids
(Id | Model | Array<(Id | Model)>)
Returns void
set
Sets the resources into the collection.
You can disable adding, changing or removing.
Parameters
-
resources
Array<{}> -
$1
any (optional, default{}
)-
$1.add
(optional, defaulttrue
) -
$1.change
(optional, defaulttrue
) -
$1.remove
(optional, defaulttrue
)
-
Returns void
build
Creates a new model instance with the given attributes
Parameters
-
attributes
{} (optional, default{}
)
Returns Model
create
Creates the model and saves it on the backend
The default behaviour is optimistic but this can be tuned.
Parameters
-
attributesOrModel
({} | Model) -
$1
any (optional, default{}
)-
$1.optimistic
(optional, defaulttrue
)
-
Returns Request
fetch
Fetches the models from the backend.
It uses set
internally so you can
use the options to disable adding, changing
or removing.
Parameters
-
options
SetOptions (optional, default{}
)
Returns Request
toJS
Returns a JSON representation of the model
primaryKey
Determine what attribute do you use as a primary id
Returns string
urlRoot
Return the base url used in
the url
method
url
Return the url for this given REST resource
Returns string
isNew
Wether the resource is new or not
We determine this asking if it contains
the primaryKey
attribute (set by the server).
Returns boolean
get
Get the attribute from the model.
Since we want to be sure changes on the schema don't fail silently we throw an error if the field does not exist.
If you want to deal with flexible schemas
use has
to check wether the field
exists.
Parameters
-
attribute
string
Returns any
has
Returns whether the given field exists for the model.
Parameters
-
attribute
string
Returns boolean
id
Get an id from the model. It will use either the backend assigned one or the client.
Returns Id
changedAttributes
Get an array with the attributes names that have changed.
changes
Gets the current changes.
Returns {}
hasChanges
If an attribute is specified, returns true if it has changes. If no attribute is specified, returns true if any attribute has changes.
Parameters
-
attribute
string
Returns boolean
reset
Replace all attributes with new data
Parameters
-
data
{}
Returns void
set
Merge the given attributes with the current ones
Parameters
-
data
{}
Returns void
fetch
Fetches the model from the backend.
Parameters
-
$0
any (optional, default{}
)$0.data
-
$0.otherOptions
...any
Returns Request
applyPatchChanges
Merges old attributes with new ones. By default it doesn't merge arrays.
Parameters
-
oldAttributes
{} -
changes
{}
Returns {}
save
Saves the resource on the backend.
If the item has a primaryKey
it updates it,
otherwise it creates the new resource.
It supports optimistic and patch updates.
TODO: Add progress
Parameters
-
attributes
{} -
$1
any (optional, default{}
)-
$1.optimistic
(optional, defaulttrue
) -
$1.patch
(optional, defaulttrue
) -
$1.keepChanges
(optional, defaulttrue
) -
$1.otherOptions
...any
-
Returns Request
destroy
Destroys the resurce on the client and requests the backend to delete it there too
Parameters
-
$0
any (optional, default{}
)-
$0.optimistic
(optional, defaulttrue
) -
$0.otherOptions
...any
-
Returns Request