Skip to content

Latest commit

 

History

History
85 lines (61 loc) · 2.25 KB

actions.md

File metadata and controls

85 lines (61 loc) · 2.25 KB

Actions

Action describes the request to be made and how it should be processed.

Default actions

// { actionName: actionConfig }
{ 
  'query': { method: 'GET' },
  'get': { method: 'GET' },
  'create': { method: 'POST' },
  'update': { method: 'PUT' },
  'delete': { method: 'DELETE' },
}

Action config

  • 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.
  • transformRequestfunction(data) – The transform function takes the http request body and returns its transformed (typically serialized) version.
  • transformResponsefunction(data) – The transform function takes the http response body and returns its transformed (typically deserialized) version.

Action definitions

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()

Action arguments

  • 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])