From 1934d1a502c76e7cf133f53734979c0a0437ed15 Mon Sep 17 00:00:00 2001 From: Christian Faber Date: Tue, 27 Nov 2018 17:12:01 +0100 Subject: [PATCH] Fixed missing execution context during update function +Test --- src/index.js | 2 +- test/index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index a216e20..21fd9f6 100644 --- a/src/index.js +++ b/src/index.js @@ -101,7 +101,7 @@ const AsyncComputed = { this.$asyncComputed[key] = { exception: null, update: () => { - watcher(getterOnly(this.$options.asyncComputed[key])()) + watcher(getterOnly(this.$options.asyncComputed[key]).apply(this)) } } setAsyncState(this.$asyncComputed[key], 'updating') diff --git a/test/index.js b/test/index.js index 35cfb35..993e342 100644 --- a/test/index.js +++ b/test/index.js @@ -809,6 +809,54 @@ test("$asyncComputed[name].update triggers re-evaluation", t => { }) }) +test("$asyncComputed[name].update has the correct execution context", t => { + t.plan(8) + let addedValue = 1 + const vm = new Vue({ + data () { + return { + valueToReturn: 1, + } + }, + asyncComputed: { + a () { + return new Promise(resolve => { + resolve(this.valueToReturn + addedValue) + }) + }, + b: { + get () { + return new Promise(resolve => { + resolve(this.valueToReturn + addedValue) + }) + }, + }, + }, + }) + + Vue.nextTick(() => { + // case 1: a is a function + t.equal(vm.a, 2) + t.equal(vm.$asyncComputed['a'].state, 'success') + // case 2: b is an object with a getter function + t.equal(vm.b, 2) + t.equal(vm.$asyncComputed['b'].state, 'success') + + addedValue = 4 + + vm.$asyncComputed['a'].update() + t.equal(vm.$asyncComputed['a'].state, 'updating') + + vm.$asyncComputed['b'].update() + t.equal(vm.$asyncComputed['b'].state, 'updating') + + Vue.nextTick(() => { + t.equal(vm.a, 5) + t.equal(vm.b, 5) + }) + }) +}) + test("Plain components with neither `data` nor `asyncComputed` still work (issue #50)", t => { t.plan(1) const vm = new Vue({