Преглед изворни кода

normalize prop options at merge time

Evan You пре 11 година
родитељ
комит
39815617a4
5 измењених фајлова са 32 додато и 22 уклоњено
  1. 6 13
      src/compiler/compile.js
  2. 1 1
      src/instance/scope.js
  3. 0 3
      src/util/misc.js
  4. 16 3
      src/util/options.js
  5. 9 2
      test/unit/specs/compiler/compile_spec.js

+ 6 - 13
src/compiler/compile.js

@@ -416,7 +416,7 @@ function makeChildLinkFn (linkFns) {
  * a props link function.
  *
  * @param {Element|DocumentFragment} el
- * @param {Array} propDescriptors
+ * @param {Array} propOptions
  * @return {Function} propsLinkFn
  */
 
@@ -425,20 +425,13 @@ var settablePathRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\[[^\[\]]+\])*$/
 var literalValueRE = /^(true|false)$|^\d.*/
 var identRE = require('../parsers/path').identRE
 
-function compileProps (el, propDescriptors) {
+function compileProps (el, propOptions) {
   var props = []
-  var i = propDescriptors.length
-  var descriptor, name, options, value, path, prop, literal, single
+  var i = propOptions.length
+  var options, name, value, path, prop, literal, single
   while (i--) {
-    descriptor = propDescriptors[i]
-    // normalize prop string/descriptor
-    if (typeof descriptor === 'object') {
-      name = descriptor.name
-      options = descriptor
-    } else {
-      name = descriptor
-      options = null
-    }
+    options = propOptions[i]
+    name = options.name
     // props could contain dashes, which will be
     // interpreted as minus calculations by the parser
     // so we need to camelize the path here

+ 1 - 1
src/instance/scope.js

@@ -94,7 +94,7 @@ exports._setData = function (newData) {
   if (props) {
     i = props.length
     while (i--) {
-      key = props[i]
+      key = props[i].name
       if (key !== '$data' && !newData.hasOwnProperty(key)) {
         newData.$set(key, oldData[key])
       }

+ 0 - 3
src/util/misc.js

@@ -10,9 +10,6 @@ var config = require('../config')
 
 exports.assertProp = function (prop, value) {
   var options = prop.options
-  if (!options) {
-    return true
-  }
   var type = options.type
   var valid = true
   var expectedType

+ 16 - 3
src/util/options.js

@@ -232,10 +232,17 @@ function guardComponents (components) {
   }
 }
 
-function guardProps (child) {
-  var props = child.props
+/**
+ * Ensure all props option syntax are normalized into the
+ * Object-based format.
+ *
+ * @param {Object} options
+ */
+
+function guardProps (options) {
+  var props = options.props
   if (_.isPlainObject(props)) {
-    child.props = Object.keys(props).map(function (key) {
+    options.props = Object.keys(props).map(function (key) {
       var val = props[key]
       if (!_.isPlainObject(val)) {
         val = { type: val }
@@ -243,6 +250,12 @@ function guardProps (child) {
       val.name = key
       return val
     })
+  } else if (_.isArray(props)) {
+    options.props = props.map(function (prop) {
+      return typeof prop === 'string'
+        ? { name: prop }
+        : prop
+    })
   }
 }
 

+ 9 - 2
test/unit/specs/compiler/compile_spec.js

@@ -161,7 +161,11 @@ if (_.inBrowser) {
           name: 'default-value',
           default: 123
         }
-      ]
+      ].map(function (p) {
+        return typeof p === 'string'
+          ? { name: p }
+          : p
+      })
       var def = Vue.options.directives._prop
       el.setAttribute('a', '1')
       el.setAttribute('data-some-attr', '{{a}}')
@@ -230,7 +234,10 @@ if (_.inBrowser) {
       var def = Vue.options.directives._prop
       el.setAttribute('a', 'hi')
       el.setAttribute('b', '{{hi}}')
-      compiler.compileAndLinkProps(vm, el, ['a', 'b'])
+      compiler.compileAndLinkProps(vm, el, [
+        { name: 'a' },
+        { name: 'b' }
+      ])
       expect(vm._bindDir.calls.count()).toBe(0)
       expect(vm._data.a).toBe('hi')
       expect(hasWarned(_, 'Cannot bind dynamic prop on a root')).toBe(true)