Skip to content
This repository was archived by the owner on Dec 26, 2018. It is now read-only.

Commit 1fabecd

Browse files
committed
1.0.0-alpha compat (fix #2)
1 parent c9e2a55 commit 1fabecd

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed

vue-async-data.js

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,71 @@
1-
var asyncData = {
2-
created: function () {
3-
if (this.$options.asyncData) {
4-
this._defineMeta('$loadingAsyncData', true)
5-
}
6-
},
7-
compiled: function () {
8-
this.reloadAsyncData()
9-
},
10-
methods: {
11-
reloadAsyncData: function () {
12-
var load = this.$options.asyncData
13-
if (load) {
14-
var self = this
15-
var resolve = function (data) {
16-
if (data) {
17-
for (var key in data) {
18-
self.$set(key, data[key])
1+
(function () {
2+
var vue // lazy bind
3+
4+
var asyncData = {
5+
created: function () {
6+
if (!vue) {
7+
console.warn('[vue-async-data] not installed!')
8+
return
9+
}
10+
if (this.$options.asyncData) {
11+
if (this._defineMeta) {
12+
// 0.12 compat
13+
this._defineMeta('$loadingAsyncData', true)
14+
} else {
15+
// ^1.0.0-alpha
16+
vue.util.defineReactive(this, '$loadingAsyncData', true)
17+
}
18+
}
19+
},
20+
compiled: function () {
21+
this.reloadAsyncData()
22+
},
23+
methods: {
24+
reloadAsyncData: function () {
25+
var load = this.$options.asyncData
26+
if (load) {
27+
var self = this
28+
var resolve = function (data) {
29+
if (data) {
30+
for (var key in data) {
31+
self.$set(key, data[key])
32+
}
1933
}
34+
self.$loadingAsyncData = false
35+
self.$emit('async-data')
2036
}
21-
self.$loadingAsyncData = false
22-
self.$emit('async-data')
23-
}
24-
var reject = function (reason) {
25-
var msg = '[vue] async data load failed'
26-
if (reason instanceof Error) {
27-
console.warn(msg)
28-
throw reason
29-
} else {
30-
console.warn(msg + ': ' + reason)
37+
var reject = function (reason) {
38+
var msg = '[vue] async data load failed'
39+
if (reason instanceof Error) {
40+
console.warn(msg)
41+
throw reason
42+
} else {
43+
console.warn(msg + ': ' + reason)
44+
}
45+
}
46+
this.$loadingAsyncData = true
47+
var res = load.call(this, resolve, reject)
48+
if (res && typeof res.then === 'function') {
49+
res.then(resolve, reject)
3150
}
32-
}
33-
this.$loadingAsyncData = true
34-
var res = load.call(this, resolve, reject)
35-
if (res && typeof res.then === 'function') {
36-
res.then(resolve, reject)
3751
}
3852
}
3953
}
4054
}
41-
}
4255

43-
var api = {
44-
mixin: asyncData,
45-
install: function (Vue, options) {
46-
Vue.options = Vue.util.mergeOptions(Vue.options, asyncData)
56+
var api = {
57+
mixin: asyncData,
58+
install: function (Vue, options) {
59+
vue = Vue
60+
Vue.options = Vue.util.mergeOptions(Vue.options, asyncData)
61+
}
4762
}
48-
}
4963

50-
if(typeof exports === 'object' && typeof module === 'object') {
51-
module.exports = api
52-
} else if(typeof define === 'function' && define.amd) {
53-
define(function () { return api })
54-
} else if (typeof window !== 'undefined') {
55-
window.VueAsyncData = api
56-
}
64+
if(typeof exports === 'object' && typeof module === 'object') {
65+
module.exports = api
66+
} else if(typeof define === 'function' && define.amd) {
67+
define(function () { return api })
68+
} else if (typeof window !== 'undefined') {
69+
window.VueAsyncData = api
70+
}
71+
})()

0 commit comments

Comments
 (0)