Explorar o código

make computed cache default to false

Evan You %!s(int64=10) %!d(string=hai) anos
pai
achega
9e56f97d44
Modificáronse 3 ficheiros con 17 adicións e 24 borrados
  1. 7 0
      src/deprecations.js
  2. 7 2
      src/instance/state.js
  3. 3 22
      test/unit/specs/instance/state_spec.js

+ 7 - 0
src/deprecations.js

@@ -138,6 +138,13 @@ if (process.env.NODE_ENV !== 'production') {
         'bound as expression in 1.0.0. ' +
         'For more details, see https://github.com/yyx990803/vue/issues/1173'
       )
+    },
+
+    COMPUTED_CACHE: function (name) {
+      warn(
+        'Computed property "' + name + '": computed properties are not cached by ' +
+        'default in 1.0.0. You only need to enable cache for particularly expensive ones.'
+      )
     }
 
   }

+ 7 - 2
src/instance/state.js

@@ -199,11 +199,16 @@ exports._initComputed = function () {
         configurable: true
       }
       if (typeof userDef === 'function') {
-        def.get = makeComputedGetter(userDef, this)
+        def.get = _.bind(userDef, this)
         def.set = noop
       } else {
+
+        if (process.env.NODE_ENV !== 'production' && userDef.cache === false) {
+          _.deprecation.COMPUTED_CACHE(key)
+        }
+
         def.get = userDef.get
-          ? userDef.cache !== false
+          ? userDef.cache
             ? makeComputedGetter(userDef.get, this)
             : _.bind(userDef.get, this)
           : noop

+ 3 - 22
test/unit/specs/instance/scope_spec.js → test/unit/specs/instance/state_spec.js

@@ -1,6 +1,6 @@
 var Vue = require('../../../../src/vue')
 
-describe('Instance Scope', function () {
+describe('Instance state initialization', function () {
 
   describe('data proxy', function () {
 
@@ -180,6 +180,7 @@ describe('Instance Scope', function () {
         },
         // cached
         f: {
+          cache: true,
           get: function () {
             spyF()
             return this.ff
@@ -191,6 +192,7 @@ describe('Instance Scope', function () {
         },
         // another cached, for watcher test
         h: {
+          cache: true,
           get: function () {
             return this.hh
           }
@@ -302,27 +304,6 @@ describe('Instance Scope', function () {
       expect(vm.d).toBe('CD')
       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 () {