Quellcode durchsuchen

computed properties should be cached by default

Evan You vor 10 Jahren
Ursprung
Commit
3fc91b970a
2 geänderte Dateien mit 22 neuen und 4 gelöschten Zeilen
  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
       }
       if (typeof userDef === 'function') {
-        def.get = _.bind(userDef, this)
+        def.get = makeComputedGetter(userDef, this)
         def.set = noop
       } else {
         def.get = userDef.get
-          ? userDef.cache
+          ? userDef.cache !== false
             ? makeComputedGetter(userDef.get, this)
             : _.bind(userDef.get, this)
           : noop

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

@@ -180,7 +180,6 @@ describe('Instance Scope', function () {
         },
         // cached
         f: {
-          cache: true,
           get: function () {
             spyF()
             return this.ff
@@ -192,7 +191,6 @@ describe('Instance Scope', function () {
         },
         // another cached, for watcher test
         h: {
-          cache: true,
           get: function () {
             return this.hh
           }
@@ -305,6 +303,26 @@ describe('Instance Scope', function () {
       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 () {