Explorar o código

computed properties should be cached by default

Evan You %!s(int64=10) %!d(string=hai) anos
pai
achega
3fc91b970a
Modificáronse 2 ficheiros con 22 adicións e 4 borrados
  1. 2 2
      src/instance/scope.js
  2. 20 2
      test/unit/specs/instance/scope_spec.js

+ 2 - 2
src/instance/scope.js

@@ -195,11 +195,11 @@ exports._initComputed = function () {
         configurable: true
         configurable: true
       }
       }
       if (typeof userDef === 'function') {
       if (typeof userDef === 'function') {
-        def.get = _.bind(userDef, this)
+        def.get = makeComputedGetter(userDef, this)
         def.set = noop
         def.set = noop
       } else {
       } else {
         def.get = userDef.get
         def.get = userDef.get
-          ? userDef.cache
+          ? userDef.cache !== false
             ? makeComputedGetter(userDef.get, this)
             ? makeComputedGetter(userDef.get, this)
             : _.bind(userDef.get, this)
             : _.bind(userDef.get, this)
           : noop
           : noop

+ 20 - 2
test/unit/specs/instance/scope_spec.js

@@ -180,7 +180,6 @@ describe('Instance Scope', function () {
         },
         },
         // cached
         // cached
         f: {
         f: {
-          cache: true,
           get: function () {
           get: function () {
             spyF()
             spyF()
             return this.ff
             return this.ff
@@ -192,7 +191,6 @@ describe('Instance Scope', function () {
         },
         },
         // another cached, for watcher test
         // another cached, for watcher test
         h: {
         h: {
-          cache: true,
           get: function () {
           get: function () {
             return this.hh
             return this.hh
           }
           }
@@ -305,6 +303,26 @@ describe('Instance Scope', function () {
       expect(vm.e).toBe('CDe')
       expect(vm.e).toBe('CDe')
     })
     })
 
 
+    it('disable cache', function () {
+      var external = { b: 'B' }
+      var vm = new Vue({
+        data: {
+          a: 'A'
+        },
+        computed: {
+          test: {
+            cache: false,
+            get: function () {
+              return this.a + external.b
+            }
+          }
+        }
+      })
+      expect(vm.test).toBe('AB')
+      external.b = 'C'
+      expect(vm.test).toBe('AC')
+    })
+
   })
   })
 
 
   describe('methods', function () {
   describe('methods', function () {