-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathform.js
73 lines (59 loc) · 1.46 KB
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Errors from './errors';
class Form {
constructor(data) {
this.originalData = data;
for ( let field in data ) {
this[field] = data[field];
}
this.errors = new Errors();
}
fill(data) {
this.originalData = data;
for ( let field in data ) {
this[field] = data[field];
}
this.errors = new Errors();
}
reset() {
for ( let field in this.originalData ) {
this[field]= '';
}
this.errors.clear();
}
data() {
let data = {};
for ( let property in this.originalData ) {
data[property] = this[property];
}
return data;
}
post(url) {
return this.submit('post',url);
}
delete(url) {
return this.submit('delete',url);
}
put(url) {
return this.submit('put',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);
reject(error.response.data);
})
});
}
onSuccess(data) {
this.reset();
}
onFail(error) {
this.errors.record(error.errors)
}
}
export default Form;