Skip to content

Commit a4bb765

Browse files
committed
extarct the initialization of the data in beforeCreate into function
1 parent e2aa217 commit a4bb765

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/index.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,7 @@ const AsyncComputed = {
4040
this.$options.computed[prefix + key] = getter
4141
}
4242

43-
this.$options.data = function vueAsyncComputedInjectedDataFn(vm) {
44-
const data = (
45-
(typeof optionData === 'function')
46-
? optionData.call(this, vm)
47-
: optionData
48-
) || {}
49-
for (const key in asyncComputed) {
50-
const item = this.$options.asyncComputed[key]
51-
if (isComputedLazy(item)) {
52-
initLazy(data, key)
53-
this.$options.computed[key] = makeLazyComputed(key)
54-
} else {
55-
data[key] = null
56-
}
57-
}
58-
return data
59-
}
43+
this.$options.data = initDataWithAsyncComputed(this.$options)
6044
},
6145
created() {
6246
for (const key in this.$options.asyncComputed || {}) {
@@ -119,6 +103,28 @@ const AsyncComputed = {
119103
}
120104
}
121105

106+
function initDataWithAsyncComputed(options) {
107+
const optionData = options.data
108+
const asyncComputed = options.asyncComputed || {}
109+
110+
return function vueAsyncComputedInjectedDataFn(vm) {
111+
const data = ((typeof optionData === 'function')
112+
? optionData.call(this, vm)
113+
: optionData) || {};
114+
for (const key in asyncComputed) {
115+
const item = this.$options.asyncComputed[key];
116+
if (isComputedLazy(item)) {
117+
initLazy(data, key);
118+
this.$options.computed[key] = makeLazyComputed(key);
119+
}
120+
else {
121+
data[key] = null;
122+
}
123+
}
124+
return data;
125+
};
126+
}
127+
122128
function setAsyncState(vm, stateObject, state) {
123129
vm.$set(vm.$data._asyncComputed[stateObject], 'state', state)
124130
vm.$set(vm.$data._asyncComputed[stateObject], 'updating', state === 'updating')

0 commit comments

Comments
 (0)