Mixins
Note: To import mixins on your components do as above:
import { ApiList, ApiForm} from 'vue-rest';
ApiList
The ApiList is responsible for implementing a route
mandatory prop and the three following methods:
loadList(callback, errCallback):
- Params: callback, errCallback
- Functionality: It uses
$api
prototype to do aGET
request in the givenroute
and pass the response for the callback function if the response was successfully made, otherwise passes the error to the errCallback function.
deleteObj(objId, callback, errCallback):
- Params: objId, callback, errCallback
- Functionality: It uses
$api
prototype to do aDELETE
request in the givenroute
concatenated with theobjId
and passes the response for the callback function if the response was successfully made, then it emits adelete
event with theobjId
.
editObj(obj):
- Params: obj
- Functionality: It just emits an
edit
event with the givenobj
in its value. Note: You should probably handle the emitted value to pass to a ApiForm instance
ApiForm
The ApiForm is responsible for implementing the methods for API requesting that usually a form does.
It requires a route
prop and, for convenience, can handle two others props objId
and obj
. Both props are handled in mixin's created()
hook, and the obj
set the requestObj
data to its value and the objId
loads a new instance of a object to requestObj
returned from API in theload
method.
See: https://vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks
The following methods are implemented through this mixin:
load(id, errCallback):
- Params: id, errCallback
- Functionality: It does a
GET
request to the givenroute
/id
and loads theresponse.data
to therequestObj
data.
save(callback, errCallback):
- Params: callback, errCallback
- Functionality: It passes the received
callback
anderrCallback
to one of the following methods:- If the
requestObj
has anid
(requestObj.id
):update(callback, errCallback):
- Params: callback, errCallback
- Functionality: It uses the
$api
prototype to do aPATCH
requests using the givenroute
and passing therequestObj
as payload, passing the success response to thecallback
function and if there's any error it passes the it to theerrCallback
function.
- Otherwise:
create(callback, errCallback):
- Params: callback, errCallback
- Functionality: It does the same thing as the update method but instead of a
PATCH
it does aPOST
to the API.
- If the