Skip to content

Commit 949349e

Browse files
Merge pull request #54 from Faberle/master
Fixed missing execution context during update function: Fixes #53
2 parents 0e6fa13 + 1934d1a commit 949349e

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const AsyncComputed = {
101101
this.$asyncComputed[key] = {
102102
exception: null,
103103
update: () => {
104-
watcher(getterOnly(this.$options.asyncComputed[key])())
104+
watcher(getterOnly(this.$options.asyncComputed[key]).apply(this))
105105
}
106106
}
107107
setAsyncState(this.$asyncComputed[key], 'updating')

test/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,54 @@ test("$asyncComputed[name].update triggers re-evaluation", t => {
809809
})
810810
})
811811

812+
test("$asyncComputed[name].update has the correct execution context", t => {
813+
t.plan(8)
814+
let addedValue = 1
815+
const vm = new Vue({
816+
data () {
817+
return {
818+
valueToReturn: 1,
819+
}
820+
},
821+
asyncComputed: {
822+
a () {
823+
return new Promise(resolve => {
824+
resolve(this.valueToReturn + addedValue)
825+
})
826+
},
827+
b: {
828+
get () {
829+
return new Promise(resolve => {
830+
resolve(this.valueToReturn + addedValue)
831+
})
832+
},
833+
},
834+
},
835+
})
836+
837+
Vue.nextTick(() => {
838+
// case 1: a is a function
839+
t.equal(vm.a, 2)
840+
t.equal(vm.$asyncComputed['a'].state, 'success')
841+
// case 2: b is an object with a getter function
842+
t.equal(vm.b, 2)
843+
t.equal(vm.$asyncComputed['b'].state, 'success')
844+
845+
addedValue = 4
846+
847+
vm.$asyncComputed['a'].update()
848+
t.equal(vm.$asyncComputed['a'].state, 'updating')
849+
850+
vm.$asyncComputed['b'].update()
851+
t.equal(vm.$asyncComputed['b'].state, 'updating')
852+
853+
Vue.nextTick(() => {
854+
t.equal(vm.a, 5)
855+
t.equal(vm.b, 5)
856+
})
857+
})
858+
})
859+
812860
test("Plain components with neither `data` nor `asyncComputed` still work (issue #50)", t => {
813861
t.plan(1)
814862
const vm = new Vue({

0 commit comments

Comments
 (0)