Skip to content

Commit 0d10602

Browse files
Merge pull request #95 from alex-dunn/master
Default values applied before watch/computed can read undefined/null value
2 parents 732afe1 + 0494f78 commit 0d10602

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const AsyncComputed = {
4848
this.$options.computed[prefix + key] = getter
4949
}
5050

51-
this.$options.data = initDataWithAsyncComputed(this.$options)
51+
this.$options.data = initDataWithAsyncComputed(this.$options, pluginOptions)
5252
},
5353
created () {
5454
for (const key in this.$options.asyncComputed || {}) {
@@ -115,7 +115,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
115115
vm.$watch(prefix + key, watcher, { immediate: true })
116116
}
117117

118-
function initDataWithAsyncComputed (options) {
118+
function initDataWithAsyncComputed (options, pluginOptions) {
119119
const optionData = options.data
120120
const asyncComputed = options.asyncComputed || {}
121121

@@ -125,11 +125,13 @@ function initDataWithAsyncComputed (options) {
125125
: optionData) || {}
126126
for (const key in asyncComputed) {
127127
const item = this.$options.asyncComputed[key]
128+
129+
var value = generateDefault.call(this, item, pluginOptions)
128130
if (isComputedLazy(item)) {
129-
initLazy(data, key)
131+
initLazy(data, key, value)
130132
this.$options.computed[key] = makeLazyComputed(key)
131133
} else {
132-
data[key] = null
134+
data[key] = value
133135
}
134136
}
135137
return data

src/lazy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export function isLazyActive (vm, key) {
1111
const lazyActivePrefix = 'async_computed$lazy_active$',
1212
lazyDataPrefix = 'async_computed$lazy_data$'
1313

14-
export function initLazy (data, key) {
14+
export function initLazy (data, key, value) {
1515
data[lazyActivePrefix + key] = false
16-
data[lazyDataPrefix + key] = null
16+
data[lazyDataPrefix + key] = value
1717
}
1818

1919
export function makeLazyComputed (key) {

test/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ test("Multiple asyncComputed objects are handled the same as normal computed pro
221221
})
222222

223223
test("Async computed values can have defaults", t => {
224-
t.plan(6)
224+
t.plan(8)
225225
const vm = new Vue({
226226
asyncComputed: {
227227
x: {
@@ -238,9 +238,27 @@ test("Async computed values can have defaults", t => {
238238
return Promise.resolve(true)
239239
}
240240
}
241-
}
241+
},
242+
watch: {
243+
x: {
244+
deep: true,
245+
immediate: true,
246+
handler (newValue, oldValue) {
247+
if (oldValue === undefined) {
248+
t.equal(newValue, false, 'watch: x should default to false')
249+
}
250+
}
251+
},
252+
},
253+
computed: {
254+
computedFromX: function () {
255+
t.equal(this.x, false, 'computed: x should default to false')
256+
return this.x
257+
},
258+
},
242259
})
243-
t.equal(vm.x, false, 'x should default to true')
260+
const computed = vm.computedFromX// Force computed execution
261+
t.equal(vm.x, false, 'x should default to false')
244262
t.equal(vm.y, null, 'y doesn\'t have a default')
245263
t.equal(vm.z, null, 'z doesn\'t have a default despite being defined with an object')
246264
Vue.nextTick(() => {

0 commit comments

Comments
 (0)