فهرست منبع

do not sync boolean literal props + cast number literal props

Evan You 11 سال پیش
والد
کامیت
14e01ebcff
2فایلهای تغییر یافته به همراه18 افزوده شده و 6 حذف شده
  1. 4 2
      src/compiler/compile.js
  2. 14 4
      test/unit/specs/compiler/compile_spec.js

+ 4 - 2
src/compiler/compile.js

@@ -392,6 +392,7 @@ function makeChildLinkFn (linkFns) {
 // regex to test if a path is "settable"
 // if not the prop binding is automatically one-way.
 var settablePathRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\[[^\[\]]+\])*$/
+var literalValueRE = /^true|false|\d+$/
 
 function compileProps (el, attrs, propNames) {
   var props = []
@@ -426,7 +427,8 @@ function compileProps (el, attrs, propNames) {
         prop.oneTime =
           tokens.length > 1 ||
           tokens[0].oneTime ||
-          !settablePathRE.test(prop.value)
+          !settablePathRE.test(prop.value) ||
+          literalValueRE.test(prop.value)
       }
       props.push(prop)
     }
@@ -469,7 +471,7 @@ function makePropsLinkFn (props) {
         }
       } else {
         // just set once
-        vm.$set(path, prop.raw)
+        vm.$set(path, _.toNumber(prop.raw))
       }
     }
   }

+ 14 - 4
test/unit/specs/compiler/compile_spec.js

@@ -154,7 +154,8 @@ if (_.inBrowser) {
           'multiple-attrs',
           'oneway',
           'with-filter',
-          'camelCase'
+          'camelCase',
+          'boolean-literal'
         ]
       })
       var def = Vue.options.directives._prop
@@ -164,11 +165,12 @@ if (_.inBrowser) {
       el.setAttribute('multiple-attrs', 'a {{b}} c')
       el.setAttribute('oneway', '{{*a}}')
       el.setAttribute('with-filter', '{{a | filter}}')
+      el.setAttribute('boolean-literal', '{{true}}')
       transclude(el, options)
       var linker = compile(el, options)
       linker(vm, el)
       // should skip literals and one-time bindings
-      expect(vm._bindDir.calls.count()).toBe(4)
+      expect(vm._bindDir.calls.count()).toBe(5)
       // data-some-attr
       var args = vm._bindDir.calls.argsFor(0)
       expect(args[0]).toBe('prop')
@@ -198,11 +200,19 @@ if (_.inBrowser) {
       expect(args[2].arg).toBe('withFilter')
       expect(args[2].expression).toBe('this._applyFilters(a,null,[{"name":"filter"}],false)')
       expect(args[3]).toBe(def)
+      // boolean-literal
+      args = vm._bindDir.calls.argsFor(4)
+      expect(args[0]).toBe('prop')
+      expect(args[1]).toBe(null)
+      expect(args[2].arg).toBe('booleanLiteral')
+      expect(args[2].expression).toBe('true')
+      expect(args[2].oneWay).toBe(true)
       // camelCase should've warn
       expect(hasWarned(_, 'using camelCase')).toBe(true)
       // literal and one time should've called vm.$set
-      expect(vm.$set).toHaveBeenCalledWith('a', '1')
-      expect(vm.$set).toHaveBeenCalledWith('someOtherAttr', '2')
+      // and numbers should be casted
+      expect(vm.$set).toHaveBeenCalledWith('a', 1)
+      expect(vm.$set).toHaveBeenCalledWith('someOtherAttr', 2)
     })
 
     it('props on root instance', function () {