-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-autorequest.js
66 lines (64 loc) · 1.78 KB
/
vue-autorequest.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
/*!
* Vue-Autorequest.js
* v1.1.0
* (c) Bryan Lim; MIT License
*/
const Autorequest = {
install(Vue) {
Vue.mixin({
data: function () {
return {
onMounted: "",
onCreated: "",
onUpdated: "",
mounted: { url: "" },
created: { url: "" },
updated: { url: "" }
}
},
updated: function(){
this.request_for("updated", this.updated.url)
},
mounted: function(){
this.request_for("mounted", this.mounted.url)
},
created: function(){
this.request_for("created", this.created.url)
},
methods: {
request_for: function(stage, endpoint){
if(endpoint == "") return
var appl = this
var request = new XMLHttpRequest();
request.open("GET", endpoint, true)
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if(this.response == undefined) return
var reply = JSON.parse(this.response)
if(stage == "updated"){
appl.onUpdated = reply
}else if(stage == "mounted"){
appl.onMounted = reply
} else if(stage == "created") {
appl.onCreated = reply
}
}
}
request.onerror = function () {
if(this.err == undefined) return
var err = JSON.parse(this.err)
if(stage == "updated"){
appl.onUpdated = err
} else if(stage == "mounted"){
appl.onMounted = err
} else if(stage == "created") {
appl.onCreated = err
}
}
request.send()
}
}
})
}
};
export default Autorequest;