Created
February 7, 2020 09:27
-
-
Save AlexPetrov7311/c0a59ebbfe08b769dc18f2125b5e5f2f to your computer and use it in GitHub Desktop.
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
| export class Form { | |
| constructor(data) { | |
| this.originalData = data; | |
| for (let field in data) { | |
| this[field] = data[field]; | |
| } | |
| } | |
| data() { | |
| let data = {}; | |
| for(let property in this.originalData) { | |
| data[property] = this[property]; | |
| } | |
| return data; | |
| } | |
| reset() { | |
| for (let field in this.originalData) { | |
| this[field] = ''; | |
| } | |
| } | |
| post(url) { | |
| return this.submit('post', url); | |
| } | |
| delete(url) { | |
| return this.submit('delete', url); | |
| } | |
| 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.data); | |
| reject(error.response.data.data); | |
| }); | |
| }); | |
| } | |
| onSuccess(data) { | |
| // | |
| } | |
| onFail(errorResponse) { | |
| // | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment