|
| 1 | +# Mixins |
| 2 | + |
| 3 | +## ApiForm |
| 4 | +### Intro |
| 5 | + |
| 6 | +The ApiForm is responsible for implementing the methods for API requesting that usually a form does. |
| 7 | + |
| 8 | +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` sets the `requestObj` data to its value and the `objId` loads a new instance of a object to `requestObj` returned from API in the `load()` method. |
| 9 | + |
| 10 | +> See: [Vue Lifecycle Hooks](https://vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks) |
| 11 | +
|
| 12 | +### Methods |
| 13 | + |
| 14 | +The following methods are implemented through this mixin: |
| 15 | + |
| 16 | +`load(id, errCallback)`: |
| 17 | + |
| 18 | +- Params: id, errCallback |
| 19 | + |
| 20 | +- Functionality: It does a GET request to the given route /id and loads the response.data to the requestObj data. |
| 21 | + |
| 22 | +`save(callback, errCallback)`: |
| 23 | +- Params: callback, errCallback |
| 24 | +- Functionality: It passes the received callback and errCallback to one of the following methods: |
| 25 | + - If the `requestObj` has an id `(requestObj.id)`: |
| 26 | + * `update(callback, errCallback)`: |
| 27 | + - Params: callback, errCallback |
| 28 | + - Functionality: It uses the $api prototype to do a PATCH requests using the given route and passing the requestObj as payload, passing the success response to the callback function and if there's any error it passes the it to the errCallback function. |
| 29 | + - Otherwise: |
| 30 | + * create(callback, errCallback): |
| 31 | + - Params: callback, errCallback |
| 32 | + - Functionality: It does the same thing as the update method but instead of a PATCH it does a POST to the API.` |
| 33 | + |
| 34 | +### Using it |
| 35 | + |
| 36 | +> Note that you need to pass a `route` prop or override the prop as: |
| 37 | +> |
| 38 | +> `route: { required: false, default: 'your-route' }` |
| 39 | +
|
| 40 | +```vue |
| 41 | +<template> |
| 42 | + <form @submit.prevent="save(showOk, showErr)"> |
| 43 | + <input v-model="requestObj.firstName"/> |
| 44 | + <input v-model="requestObj.lastName"/> |
| 45 | + <button type="submit"> |
| 46 | + Add |
| 47 | + </button> |
| 48 | + </form> |
| 49 | +</template> |
| 50 | +
|
| 51 | +<script> |
| 52 | +import { ApiForm } from 'vue-rest' |
| 53 | +
|
| 54 | +export default { |
| 55 | + name: 'personForm', |
| 56 | + mixins: [ApiForm], |
| 57 | + methods: { |
| 58 | + showOk(res) { |
| 59 | + console.log(res) |
| 60 | + alert('Sucessfully saved') |
| 61 | + }, |
| 62 | + showErr(err) { |
| 63 | + console.log(err) |
| 64 | + alert(`An error ocurred: ${err}`) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | +</script> |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +## ApiList |
| 73 | +### Intro |
| 74 | + |
| 75 | +The ApiList is responsible for implementing a route mandatory prop and the three following methods to load and handle list actions: |
| 76 | + |
| 77 | + |
| 78 | +### Methods |
| 79 | + |
| 80 | +`loadList(callback, errCallback)`: |
| 81 | + |
| 82 | +- Params: callback, errCallback |
| 83 | +- Functionality: It uses $api prototype to do a GET request in the given route and pass the response for the callback function if the response was successfully made, otherwise passes the error to the errCallback function. |
| 84 | + |
| 85 | +`deleteObj(objId, callback, errCallback)`: |
| 86 | + |
| 87 | +- Params: objId, callback, errCallback |
| 88 | +- Functionality: It uses $api prototype to do a DELETE request in the given route concatenated with the objIdand passes the response for the callback function if the response was successfully made, then it emits a delete event with the objId. |
| 89 | + |
| 90 | +`editObj(obj)`: |
| 91 | + |
| 92 | +- Params: obj |
| 93 | +- Functionality: It just emits an edit event with the given obj in its value. |
| 94 | + |
| 95 | +> Note: You should probably handle the emitted value to pass to a ApiForm instance |
| 96 | +
|
| 97 | + |
| 98 | +### Using it |
| 99 | +```vue |
| 100 | +<template> |
| 101 | + <ul> |
| 102 | + <li v-for="p in people" :key="p.id"> |
| 103 | + {{ p.firstName }} {{ p.lastName }} |
| 104 | + </li> |
| 105 | + </ul> |
| 106 | +</template> |
| 107 | +
|
| 108 | +<script> |
| 109 | +import { ApiList } from 'vue-rest' |
| 110 | +
|
| 111 | +export default { |
| 112 | + name: 'peopleList', |
| 113 | + mixins: [ApiList], |
| 114 | + data() { |
| 115 | + return { |
| 116 | + people: [], |
| 117 | + } |
| 118 | + }, |
| 119 | + created() { |
| 120 | + this.loadList(this.loadPeople, this.showErr) |
| 121 | + }, |
| 122 | + methods: { |
| 123 | + loadPeople(res) { |
| 124 | + console.log(res) |
| 125 | + this.people = [...res.data.results] |
| 126 | + }, |
| 127 | + showErr(err) { |
| 128 | + console.log(err) |
| 129 | + alert(`An error ocurred ${err}`) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +
|
| 134 | +</script> |
| 135 | +
|
| 136 | +``` |
0 commit comments