Browse Source

add coerce option for component props

Hsiaoming Yang 10 years ago
parent
commit
e10b8b0cad
2 changed files with 20 additions and 1 deletions
  1. 2 1
      src/directives/internal/prop.js
  2. 18 0
      src/util/component.js

+ 2 - 1
src/directives/internal/prop.js

@@ -5,7 +5,7 @@
 
 
 import Watcher from '../../watcher'
 import Watcher from '../../watcher'
 import config from '../../config'
 import config from '../../config'
-import { assertProp, initProp } from '../../util/index'
+import { assertProp, initProp, coerceProp } from '../../util/index'
 
 
 const bindingModes = config._propBindingModes
 const bindingModes = config._propBindingModes
 
 
@@ -25,6 +25,7 @@ export default {
       parent,
       parent,
       parentKey,
       parentKey,
       function (val) {
       function (val) {
+        val = coerceProp(prop, val)
         if (assertProp(prop, val)) {
         if (assertProp(prop, val)) {
           child[childKey] = val
           child[childKey] = val
         }
         }

+ 18 - 0
src/util/component.js

@@ -76,6 +76,7 @@ function getIsBinding (el) {
 
 
 export function initProp (vm, prop, value) {
 export function initProp (vm, prop, value) {
   const key = prop.path
   const key = prop.path
+  value = coerceProp(prop, value)
   vm[key] = vm._data[key] = assertProp(prop, value)
   vm[key] = vm._data[key] = assertProp(prop, value)
     ? value
     ? value
     : undefined
     : undefined
@@ -143,6 +144,23 @@ export function assertProp (prop, value) {
   return true
   return true
 }
 }
 
 
+/**
+ * Force parsing value with coerce option.
+ *
+ * @param {*} value
+ * @param {Object} options
+ * @return {*}
+ */
+
+export function coerceProp (prop, value) {
+  var coerce = prop.options.coerce
+  if (!coerce) {
+    return value
+  }
+  // coerce is a function
+  return coerce(value)
+}
+
 function formatType (val) {
 function formatType (val) {
   return val
   return val
     ? val.charAt(0).toUpperCase() + val.slice(1)
     ? val.charAt(0).toUpperCase() + val.slice(1)