-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-async-data.js
71 lines (68 loc) · 1.86 KB
/
vue-async-data.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
(function () {
var vue // lazy bind
var asyncData = {
created: function () {
if (!vue) {
console.warn('[vue-async-data] not installed!')
return
}
if (this.$options.asyncData) {
if (this._defineMeta) {
// 0.12 compat
this._defineMeta('$loadingAsyncData', true)
} else {
// ^1.0.0-alpha
vue.util.defineReactive(this, '$loadingAsyncData', true)
}
}
},
compiled: function () {
this.reloadAsyncData()
},
methods: {
reloadAsyncData: function () {
var load = this.$options.asyncData
if (load) {
var self = this
var resolve = function (data) {
if (data) {
for (var key in data) {
self.$set(key, data[key])
}
}
self.$loadingAsyncData = false
self.$emit('async-data')
}
var reject = function (reason) {
var msg = '[vue] async data load failed'
if (reason instanceof Error) {
console.warn(msg)
throw reason
} else {
console.warn(msg + ': ' + reason)
}
}
this.$loadingAsyncData = true
var res = load.call(this, resolve, reject)
if (res && typeof res.then === 'function') {
res.then(resolve, reject)
}
}
}
}
}
var api = {
mixin: asyncData,
install: function (Vue, options) {
vue = Vue
Vue.options = Vue.util.mergeOptions(Vue.options, asyncData)
}
}
if(typeof exports === 'object' && typeof module === 'object') {
module.exports = api
} else if(typeof define === 'function' && define.amd) {
define(function () { return api })
} else if (typeof window !== 'undefined') {
window.VueAsyncData = api
}
})()