Skip to content

Commit dbb3fac

Browse files
committed
make computed property cache optional (close vuejs#1189)
1 parent 8ef1856 commit dbb3fac

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

src/instance/scope.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,13 @@ exports._initComputed = function () {
195195
configurable: true
196196
}
197197
if (typeof userDef === 'function') {
198-
def.get = makeComputedGetter(userDef, this)
198+
def.get = _.bind(userDef, this)
199199
def.set = noop
200200
} else {
201201
def.get = userDef.get
202-
? makeComputedGetter(userDef.get, this)
202+
? userDef.cache
203+
? makeComputedGetter(userDef.get, this)
204+
: _.bind(userDef.get, this)
203205
: noop
204206
def.set = userDef.set
205207
? _.bind(userDef.set, this)

test/unit/specs/instance/scope_spec.js

+65-7
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ describe('Instance Scope', function () {
155155

156156
describe('computed', function () {
157157

158+
var spyE = jasmine.createSpy('computed e')
159+
var spyF = jasmine.createSpy('cached computed f')
160+
var spyCachedWatcher = jasmine.createSpy('cached computed watcher')
161+
158162
var Test = Vue.extend({
159163
computed: {
160164
c: function () {
@@ -173,20 +177,42 @@ describe('Instance Scope', function () {
173177
// chained computed
174178
e: function () {
175179
return this.c + 'e'
180+
},
181+
// cached
182+
f: {
183+
cache: true,
184+
get: function () {
185+
spyF()
186+
return this.ff
187+
}
188+
},
189+
// chained cached
190+
g: function () {
191+
return this.f + 1
192+
},
193+
// another cached, for watcher test
194+
h: {
195+
cache: true,
196+
get: function () {
197+
return this.hh
198+
}
176199
}
177200
}
178201
})
179202

180-
var spy = jasmine.createSpy()
181203
var vm = new Test({
182204
data: {
183205
a: 'a',
184-
b: 'b'
206+
b: 'b',
207+
ff: 0,
208+
hh: 0
209+
},
210+
watch: {
211+
e: spyE,
212+
h: spyCachedWatcher
185213
}
186214
})
187215

188-
vm.$watch('e', spy)
189-
190216
it('get', function () {
191217
expect(vm.c).toBe('ab')
192218
expect(vm.d).toBe('ab')
@@ -202,7 +228,7 @@ describe('Instance Scope', function () {
202228
expect(vm.d).toBe('cd')
203229
expect(vm.e).toBe('cde')
204230
Vue.nextTick(function () {
205-
expect(spy).toHaveBeenCalledWith('cde', 'abe')
231+
expect(spyE).toHaveBeenCalledWith('cde', 'abe')
206232
done()
207233
})
208234
})
@@ -225,13 +251,45 @@ describe('Instance Scope', function () {
225251
expect(child.d).toBe('ef')
226252
expect(vm.e).toBe('efe')
227253
Vue.nextTick(function () {
228-
expect(spy).toHaveBeenCalledWith('efe', 'cde')
254+
expect(spyE).toHaveBeenCalledWith('efe', 'cde')
255+
done()
256+
})
257+
})
258+
259+
it('cached computed', function () {
260+
expect(spyF).not.toHaveBeenCalled()
261+
var f = vm.f
262+
var g = vm.g
263+
expect(spyF.calls.count()).toBe(1)
264+
expect(f).toBe(0)
265+
expect(g).toBe(1)
266+
// get again
267+
f = vm.f
268+
g = vm.g
269+
// should not be evaluated again
270+
expect(spyF.calls.count()).toBe(1)
271+
expect(f).toBe(0)
272+
expect(g).toBe(1)
273+
// update dep
274+
vm.ff = 1
275+
f = vm.f
276+
g = vm.g
277+
expect(spyF.calls.count()).toBe(2)
278+
expect(f).toBe(1)
279+
expect(g).toBe(2)
280+
})
281+
282+
it('watching cached computed', function (done) {
283+
expect(spyCachedWatcher).not.toHaveBeenCalled()
284+
vm.hh = 2
285+
Vue.nextTick(function () {
286+
expect(spyCachedWatcher).toHaveBeenCalledWith(2, 0)
229287
done()
230288
})
231289
})
232290

233291
it('same definition object bound to different instance', function () {
234-
vm = new Test({
292+
var vm = new Test({
235293
data: {
236294
a: 'A',
237295
b: 'B'

0 commit comments

Comments
 (0)