Evan You 10 лет назад
Родитель
Сommit
925cdc5fcd
6 измененных файлов с 74 добавлено и 43 удалено
  1. 2 2
      examples/tree/tree.js
  2. 5 2
      src/api/data.js
  3. 1 1
      src/deprecations.js
  4. 40 24
      src/observer/object.js
  5. 25 13
      src/parsers/path.js
  6. 1 1
      src/util/options.js

+ 2 - 2
examples/tree/tree.js

@@ -52,7 +52,7 @@ Vue.component('item', {
     },
     },
     changeType: function () {
     changeType: function () {
       if (!this.isFolder) {
       if (!this.isFolder) {
-        this.model.$add('children', [])
+        this.model.$set('children', [])
         this.addChild()
         this.addChild()
         this.open = true
         this.open = true
       }
       }
@@ -71,4 +71,4 @@ var demo = new Vue({
   data: {
   data: {
     treeData: data
     treeData: data
   }
   }
-})
+})

+ 5 - 2
src/api/data.js

@@ -38,14 +38,17 @@ exports.$set = function (exp, val) {
 }
 }
 
 
 /**
 /**
- * Add a property on the VM
+ * Add a property on the VM (deorecated)
  *
  *
  * @param {String} key
  * @param {String} key
  * @param {*} val
  * @param {*} val
  */
  */
 
 
 exports.$add = function (key, val) {
 exports.$add = function (key, val) {
-  this._data.$add(key, val)
+  this._data.$set(key, val)
+  if (process.env.NODE_ENV !== 'production') {
+    require('../util').deprecation.ADD()
+  }
 }
 }
 
 
 /**
 /**

+ 1 - 1
src/deprecations.js

@@ -24,7 +24,7 @@ if (process.env.NODE_ENV !== 'production') {
     },
     },
 
 
     CONTENT_SELECT: function () {
     CONTENT_SELECT: function () {
-      warn('<content select="..."> will be deprecated in in 1.0.0. in favor of <content slot="...">.')
+      warn('<content select="..."> will be deprecated in in 1.0.0. in favor of <slot name="...">.')
     }
     }
 
 
   }
   }

+ 40 - 24
src/observer/object.js

@@ -2,40 +2,56 @@ var _ = require('../util')
 var objProto = Object.prototype
 var objProto = Object.prototype
 
 
 /**
 /**
- * Add a new property to an observed object
- * and emits corresponding event
- *
- * @param {String} key
- * @param {*} val
- * @public
+ * $add deprecation warning
  */
  */
 
 
 _.define(
 _.define(
   objProto,
   objProto,
   '$add',
   '$add',
-  function $add (key, val) {
+  function (key, val) {
     if (process.env.NODE_ENV !== 'production') {
     if (process.env.NODE_ENV !== 'production') {
       _.deprecation.ADD()
       _.deprecation.ADD()
     }
     }
-    if (this.hasOwnProperty(key)) return
-    var ob = this.__ob__
-    if (!ob || _.isReserved(key)) {
-      this[key] = val
-      return
-    }
-    ob.convert(key, val)
-    ob.notify()
-    if (ob.vms) {
-      var i = ob.vms.length
-      while (i--) {
-        var vm = ob.vms[i]
-        vm._proxy(key)
-        vm._digest()
-      }
-    }
+    add(this, key, val)
   }
   }
 )
 )
 
 
+/**
+ * Add a new property to an observed object
+ * and emits corresponding event. This is internal and
+ * no longer exposed as of 1.0.
+ *
+ * @param {Object} obj
+ * @param {String} key
+ * @param {*} val
+ * @public
+ */
+
+var add = exports.add = function (obj, key, val) {
+  if (obj.hasOwnProperty(key)) {
+    return
+  }
+  if (obj._isVue) {
+    add(obj._data, key, val)
+    return
+  }
+  var ob = obj.__ob__
+  if (!ob || _.isReserved(key)) {
+    obj[key] = val
+    return
+  }
+  ob.convert(key, val)
+  ob.notify()
+  if (ob.vms) {
+    var i = ob.vms.length
+    while (i--) {
+      var vm = ob.vms[i]
+      vm._proxy(key)
+      vm._digest()
+    }
+  }
+}
+
 /**
 /**
  * Set a property on an observed object, calling add to
  * Set a property on an observed object, calling add to
  * ensure the property is observed.
  * ensure the property is observed.
@@ -49,7 +65,7 @@ _.define(
   objProto,
   objProto,
   '$set',
   '$set',
   function $set (key, val) {
   function $set (key, val) {
-    this.$add(key, val)
+    add(this, key, val)
     this[key] = val
     this[key] = val
   }
   }
 )
 )

+ 25 - 13
src/parsers/path.js

@@ -1,4 +1,5 @@
 var _ = require('../util')
 var _ = require('../util')
+var add = require('../observer/object').add
 var Cache = require('../cache')
 var Cache = require('../cache')
 var pathCache = new Cache(1000)
 var pathCache = new Cache(1000)
 var identRE = exports.identRE = /^[$_a-zA-Z]+[\w$]*$/
 var identRE = exports.identRE = /^[$_a-zA-Z]+[\w$]*$/
@@ -294,6 +295,22 @@ exports.get = function (obj, path) {
   }
   }
 }
 }
 
 
+/**
+ * Warn against setting non-existent root path on a vm.
+ */
+
+var warnNonExistent
+if (process.env.NODE_ENV !== 'production') {
+  warnNonExistent = function (path) {
+    _.warn(
+      'You are setting a non-existent path "' + path.raw + '" ' +
+      'on a vm instance. Consider pre-initializing the property ' +
+      'with the "data" option for more reliable reactivity ' +
+      'and better performance.'
+    )
+  }
+}
+
 /**
 /**
  * Set on an object from a path
  * Set on an object from a path
  *
  *
@@ -320,9 +337,11 @@ exports.set = function (obj, path, val) {
     if (i < l - 1) {
     if (i < l - 1) {
       obj = obj[key]
       obj = obj[key]
       if (!_.isObject(obj)) {
       if (!_.isObject(obj)) {
-        warnNonExistent(path)
         obj = {}
         obj = {}
-        last.$add(key, obj)
+        if (process.env.NODE_ENV !== 'production' && last._isVue) {
+          warnNonExistent(path)
+        }
+        add(last, key, obj)
       }
       }
     } else {
     } else {
       if (_.isArray(obj)) {
       if (_.isArray(obj)) {
@@ -330,19 +349,12 @@ exports.set = function (obj, path, val) {
       } else if (key in obj) {
       } else if (key in obj) {
         obj[key] = val
         obj[key] = val
       } else {
       } else {
-        warnNonExistent(path)
-        obj.$add(key, val)
+        if (process.env.NODE_ENV !== 'production' && obj._isVue) {
+          warnNonExistent(path)
+        }
+        add(obj, key, val)
       }
       }
     }
     }
   }
   }
   return true
   return true
 }
 }
-
-function warnNonExistent (path) {
-  process.env.NODE_ENV !== 'production' && _.warn(
-    'You are setting a non-existent path "' + path.raw + '" ' +
-    'on a vm instance. Consider pre-initializing the property ' +
-    'with the "data" option for more reliable reactivity ' +
-    'and better performance.'
-  )
-}

+ 1 - 1
src/util/options.js

@@ -26,7 +26,7 @@ function mergeData (to, from) {
     toVal = to[key]
     toVal = to[key]
     fromVal = from[key]
     fromVal = from[key]
     if (!to.hasOwnProperty(key)) {
     if (!to.hasOwnProperty(key)) {
-      to.$add(key, fromVal)
+      to.$set(key, fromVal)
     } else if (_.isObject(toVal) && _.isObject(fromVal)) {
     } else if (_.isObject(toVal) && _.isObject(fromVal)) {
       mergeData(toVal, fromVal)
       mergeData(toVal, fromVal)
     }
     }