# Api Calls

The model has the capability to interact with your api. The exact behaviour depends on the ApiCaller implementation used. To facilitate some higher level methods like touch the model provides methods equivalent to the http methods.

# Properties

# endpoint

The endpoint property is a getter which should return a string that is the or part of the address which the model can interact with. If not setting this value, the endpoint will be guessed for you. This value will be appended to the baseEndPoint from the GlobalConfig if the baseEndPoint has been set.

# loading

The loading property indicates whether there is an ongoing request on the model you're currently interacting with.

# serverAttributeCasing

The serverAttributeCasing is a getter which similarly to attributeCasing casts the request keys to the given casing on outgoing requests. The valid values are 'snake' or 'camel' with 'snake' being the default value.

# _lastSyncedAt

The _lastSyncedAt or _last_synced_at (naming subject to attributeCasing) attribute is a getter attribute that is set only when the model data has been fetched, saved or refreshed. It is a javascript Date (opens new window) object indicating when was the last time the data has been loaded from the backend.

# Methods

TIP

All request methods on success will call the resetEndpoint and will reset the all the query parameters.

# get

async

The get method initiates a new GET request. It optionally accepts an object which are passed to the ApiCaller to transform into a query string. The method is also available statically. It returns a Model or ModelCollection.

import User from '@Models/User';

const user = new User;
user.get();
User.get();
1
2
3
4
5

# post

async

The post method initiates a new POST request. It returns a Model if the endpoint returns data otherwise returns itself.

import User from '@Models/User';

const user = new User;
user.post({ attribute: 1 });
1
2
3
4

# put

async

The put method initiates a new PUT request. It returns a Model if the endpoint returns data otherwise returns itself.

import User from '@Models/User';

const user = new User;
user.put({ attribute: 1 });
1
2
3
4

# patch

async

The patch method initiates a new PATCH request. It returns a Model if the endpoint returns data otherwise returns itself.

import User from '@Models/User';

const user = new User;
user.patch({ attribute: 1 });
1
2
3
4

# delete

async

The delete method initiates a new DELETE request. It returns a Model if the endpoint returns data otherwise returns itself.

import User from '@Models/User';

const user = new User;
user.delete({ attribute: 1 });
1
2
3
4

# Endpoint manipulation

There are couple utilities to change the endpoint for the next request.

# setEndpoint

The setEndpoint method replaces the endpoint for the next request.

import User from '@Models/User';

const user = new User;
user.getEndpoint(); // 'users'
user.setEndpoint('/something').getEndpoint(); // '/something'
1
2
3
4
5

# getEndpoint

The getEndpoint method returns the current endpoint.

import User from '@Models/User';

const user = new User;
user.getEndpoint(); // 'users'
1
2
3
4

# resetEndpoint

The resetEndpoint method resets the endpoint to the original endpoint. If endpoint is not set, it will try to guess it based on the model name.

import User from '@Models/User';

const user = new User;
user.setEndpoint('/something').getEndpoint(); // '/something'
user.resetEndpoint('/something').getEndpoint(); // 'users'
1
2
3
4
5

# appendToEndpoint

The appendToEndpoint methods appends the given string to the current endpoint.

import User from '@Models/User';

const user = new User;
user.getEndpoint(); // 'users'
user.appendToEndpoint('/something').getEndpoint(); // 'users/something'
1
2
3
4
5