Created
September 30, 2018 17:05
-
-
Save Miaababikir/6298e47b18f0c9e5822d33d23da0c710 to your computer and use it in GitHub Desktop.
Small framework for dealing with ajax request using vue and axios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Form { | |
| /** | |
| * Initialize the form data | |
| * | |
| * @param {object} data | |
| **/ | |
| constructor(data) { | |
| this.orignalData = data; | |
| for (let field in data) { | |
| this[field] = data[field]; | |
| } | |
| this.errors = new Errors(); | |
| } | |
| /** | |
| * Prepare the data to be submitted | |
| * | |
| * @returns {object} | |
| **/ | |
| data() { | |
| let data = {}; | |
| for (let property in this.orignalData) { | |
| data[property] = this[property]; | |
| } | |
| return data; | |
| } | |
| /** | |
| * Reset the form field | |
| */ | |
| reset() { | |
| for (let field in this.orignalData) { | |
| this[field] = ''; | |
| } | |
| this.errors.clear(); | |
| } | |
| /** | |
| * To make a post request | |
| * | |
| * @param url | |
| * | |
| * @returns {*} | |
| */ | |
| post(url) { | |
| return this.submit('post', url); | |
| } | |
| /** | |
| * To make a patch request | |
| * | |
| * @param url | |
| * | |
| * @returns {*} | |
| */ | |
| patch(url) { | |
| return this.submit('patch', url); | |
| } | |
| /** | |
| * To make a delete request | |
| * | |
| * @param url | |
| * | |
| * @returns {*} | |
| */ | |
| delete(url) { | |
| return this.submit('delete', url); | |
| } | |
| /** | |
| * Submitting the form | |
| * | |
| * @param {string} requestType | |
| * @param {string} url | |
| * @returns {*} | |
| */ | |
| submit(requestType, url) { | |
| return new Promise((resolve, reject) => { | |
| axios[requestType](url, this.data()) | |
| .then(response => { | |
| this.onSuccess(response.data); | |
| resolve(response.data); | |
| }) | |
| .catch(error => { | |
| this.onFail(error.response.data.errors); | |
| reject(error.response.data); | |
| }) | |
| }); | |
| } | |
| /** | |
| * Making success message | |
| * | |
| * @param data | |
| */ | |
| onSuccess(data) { | |
| alert(data.message); | |
| this.reset(); | |
| } | |
| /** | |
| * Showing the errors | |
| * | |
| * @param errors | |
| */ | |
| onFail(errors) { | |
| this.errors.record(errors) | |
| } | |
| } | |
| class Errors { | |
| /** | |
| * Initializing | |
| */ | |
| constructor() { | |
| this.errors = {}; | |
| } | |
| /** | |
| * Checking if the there is error | |
| * | |
| * @param field | |
| * | |
| * @returns {boolean} | |
| */ | |
| has(field) { | |
| return this.errors.hasOwnProperty(field); | |
| } | |
| /** | |
| * Get the error by the field name | |
| * | |
| * @param field | |
| * | |
| * @returns {*} | |
| */ | |
| get(field) { | |
| if (this.errors[field]) { | |
| return this.errors[field][0]; | |
| } | |
| } | |
| /** | |
| * Check if there is any error | |
| * | |
| * @returns {boolean} | |
| */ | |
| any() { | |
| return Object.keys(this.errors).length > 0; | |
| } | |
| /** | |
| * Record the errors | |
| * | |
| * @param errors | |
| */ | |
| record(errors) { | |
| this.errors = errors; | |
| } | |
| clear(field) { | |
| if (field) { | |
| delete this.errors[field]; | |
| return; | |
| } | |
| this.errors = {}; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment