Action describes the request to be made and how it should be processed.
// { actionName: actionConfig }
{
'query': { method: 'GET' },
'get': { method: 'GET' },
'create': { method: 'POST' },
'update': { method: 'PUT' },
'delete': { method: 'DELETE' },
}
- method –
{string}
– HTTP method (e.g.GET
,POST
, etc) - url –
{string}
– Absolute or relative URL of the resource that is being requested. - params –
{Object}
– Params object will be turned to?key1=value1&key2=value2
after the url. - transformRequest –
function(data)
– The transform function takes the http request body and returns its transformed (typically serialized) version. - transformResponse –
function(data)
– The transform function takes the http response body and returns its transformed (typically deserialized) version.
Each action is defined as:
-
Class method
// Create `User` model class const User = new ReactResource('/api/users/{:id}', {id: ':id'}); // Call `get` action on class User.get({id: 1})
-
Instance method
Prefixed with
$
dollar sign// Create `User` model class const User = new ReactResource('/api/users/{:id}', {id: ':id'}); // Create `User` instance const user = new User({id: 1}); // Call `get` action on instance user.$get()
- initObject - {Object} - data of
Model
instance - params - {Object} - action url query params
- body - {Object} - action request body.
- resolveFn - {function(response)} - successfull request callback function
- rejectFn - {function(rejection)} - rejected request callback function
There are two types of arguments list:
-
Full (first argument is Object)
// Class method action Model.create(initObject, [params, [body, [resolveFn, [rejectFn]]]]) // Instance method action model = new Model(initObject); model.$create(params, [body, [resolveFn, [rejectFn]]])
-
Callbacks (first argument is Function)
// Class method action Model.query(resolveFn, [rejectFn]) // Instance method action model = new Model(); model.$query(resolveFn, [rejectFn])