Skip to content

Commit 7d0cd9b

Browse files
Use for/in instead of forEach on Object.keys
Because `Object.keys` doesn't enumerate keys all the way up the prototype chain, this was causing bugs when used with `Vue.extend`.
1 parent 7db8e68 commit 7d0cd9b

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
33
# Changelog
44

5+
- [v3.1.1](#v311)
56
- [v3.1.0](#v310)
67
- [v3.0.1](#v301)
78
- [v3.0.0](#v300)
@@ -15,6 +16,10 @@
1516

1617
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1718

19+
### v3.1.1
20+
* Fix bug where `vue-async-computed` wouldn't find async computed
21+
properties that were further up the prototype chain.
22+
1823
### v3.1.0
1924
* Add option for setting a global default value
2025
* Improve test coverage

src/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@ const AsyncComputed = {
1414

1515
if (!this.$options.computed) this.$options.computed = {}
1616

17-
Object.keys(this.$options.asyncComputed || {}).forEach(key => {
17+
for (const key in this.$options.asyncComputed || {}) {
1818
this.$options.computed[prefix + key] = getterFor(this.$options.asyncComputed[key])
19-
})
19+
}
2020

2121
this.$options.data = function vueAsyncComputedInjectedDataFn () {
2222
const data = (
2323
(typeof optionData === 'function')
2424
? optionData.call(this)
2525
: optionData
2626
) || {}
27-
Object.keys(this.$options.asyncComputed || {}).forEach(key => {
27+
for (const key in this.$options.asyncComputed || {}) {
2828
data[key] = null
29-
})
29+
}
3030
return data
3131
}
3232
},
3333
created () {
34-
Object.keys(this.$options.asyncComputed || {}).forEach(key => {
34+
for (const key in this.$options.asyncComputed || {}) {
3535
this[key] = defaultFor.call(this, this.$options.asyncComputed[key], pluginOptions)
36-
})
36+
}
3737

38-
Object.keys(this.$options.asyncComputed || {}).forEach(key => {
38+
for (const key in this.$options.asyncComputed || {}) {
3939
let promiseId = 0
4040
this.$watch(prefix + key, newPromise => {
4141
const thisPromise = ++promiseId
@@ -63,7 +63,7 @@ const AsyncComputed = {
6363
}
6464
})
6565
}, { immediate: true })
66-
})
66+
}
6767
}
6868
})
6969
}

test/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,20 @@ test("Handle an async computed value returning synchronously", t => {
396396
t.equal(vm.x, 1)
397397
})
398398
})
399+
400+
test("Work correctly with Vue.extend", t => {
401+
t.plan(2)
402+
const SubVue = Vue.extend({
403+
asyncComputed: {
404+
async x () {
405+
return 1
406+
}
407+
}
408+
})
409+
const vm = new SubVue({})
410+
411+
t.equal(vm.x, null)
412+
Vue.nextTick(() => {
413+
t.equal(vm.x, 1)
414+
})
415+
})

0 commit comments

Comments
 (0)