Installation
Lore-Actions is available as an npm package.
npm install lore-actions
After npm install, you'll find all the .js files in the /src folder and their compiled versions in the /lib folder.
Usage
lore-actions is an abstraction teir to reduce the boilerplate associated with making ajax calls in client side applications that use React and Redux. It works by defining a set of blueprints for standard REST calls for CRUD operations, and then leveraging those blueprints using a configuration file. Example configuration file:
moduleexports = blueprint: 'create' model: Todo optimistic: actionType: ActionTypesADD_TODO payloadState: PayloadStatesCREATING onSuccess: actionType: ActionTypesUPDATE_TODO payloadState: PayloadStatesRESOLVED onError: actionType: ActionTypesREMOVE_TODO payloadState: PayloadStatesERROR_CREATING { console; } ;
Configuration options are as follows:
blueprint
[required]: can be one of create
, destroy
, fetch
, or update
model
[required]: A Backbone Model or Collection, or any function/object that abides by the Backbone interface (has fetch()
and save()
methods that return a promise).
optimistic
[optional]: Action to emit before making an AJAX call to the server. Must be specified as a pair of strings called actionType
and payloadState
. These values will be picked up by the Redux reducers. actionType
should be something like ADD_TODO
or REMOVE_TODO
(value is arbitrary but should be unique). payloadState
should be something like FETCHING
or ERROR_CREATING
.
onSuccess
[optional]: Action to emit if the AJAX call to the server is successful (returns a 200 level status code). Arguments are same as optimistic
option.
onError
[optional]: Action to emit if the AJAX call to the server fails (returns a non-200 level status code). Supports an optional beforeDispatch
callback that allows you to handle the error case by notifying the user (such as through the console or through a toast or snack in the UI).