@@ -26,19 +26,23 @@ export default function model (
2626 }
2727 }
2828 if ( tag === 'select' ) {
29- genSelect ( el , value )
29+ genSelect ( el , value , modifiers )
3030 } else if ( tag === 'input' && type === 'checkbox' ) {
31- genCheckboxModel ( el , value )
31+ genCheckboxModel ( el , value , modifiers )
3232 } else if ( tag === 'input' && type === 'radio' ) {
33- genRadioModel ( el , value )
33+ genRadioModel ( el , value , modifiers )
3434 } else {
3535 genDefaultModel ( el , value , modifiers )
3636 }
3737 // ensure runtime directive metadata
3838 return true
3939}
4040
41- function genCheckboxModel ( el : ASTElement , value : string ) {
41+ function genCheckboxModel (
42+ el : ASTElement ,
43+ value : string ,
44+ modifiers : ?ASTModifiers
45+ ) {
4246 if ( process . env . NODE_ENV !== 'production' &&
4347 el . attrsMap . checked != null ) {
4448 warn (
@@ -47,6 +51,7 @@ function genCheckboxModel (el: ASTElement, value: string) {
4751 'Declare initial values in the component\'s data option instead.'
4852 )
4953 }
54+ const number = modifiers && modifiers . number
5055 const valueBinding = getBindingAttr ( el , 'value' ) || 'null'
5156 const trueValueBinding = getBindingAttr ( el , 'true-value' ) || 'true'
5257 const falseValueBinding = getBindingAttr ( el , 'false-value' ) || 'false'
@@ -60,7 +65,7 @@ function genCheckboxModel (el: ASTElement, value: string) {
6065 '$$el=$event.target,' +
6166 `$$c=$$el.checked?(${ trueValueBinding } ):(${ falseValueBinding } );` +
6267 'if(Array.isArray($$a)){' +
63- `var $$v=${ valueBinding } ,` +
68+ `var $$v=${ number ? '_n(' + valueBinding + ')' : valueBinding } ,` +
6469 '$$i=_i($$a,$$v);' +
6570 `if($$c){$$i<0&&(${ value } =$$a.concat($$v))}` +
6671 `else{$$i>-1&&(${ value } =$$a.slice(0,$$i).concat($$a.slice($$i+1)))}` +
@@ -69,7 +74,11 @@ function genCheckboxModel (el: ASTElement, value: string) {
6974 )
7075}
7176
72- function genRadioModel ( el : ASTElement , value : string ) {
77+ function genRadioModel (
78+ el : ASTElement ,
79+ value : string ,
80+ modifiers : ?ASTModifiers
81+ ) {
7382 if ( process . env . NODE_ENV !== 'production' &&
7483 el . attrsMap . checked != null ) {
7584 warn (
@@ -78,15 +87,17 @@ function genRadioModel (el: ASTElement, value: string) {
7887 'Declare initial values in the component\'s data option instead.'
7988 )
8089 }
81- const valueBinding = getBindingAttr ( el , 'value' ) || 'null'
90+ const number = modifiers && modifiers . number
91+ let valueBinding = getBindingAttr ( el , 'value' ) || 'null'
92+ valueBinding = number ? `_n(${ valueBinding } )` : valueBinding
8293 addProp ( el , 'checked' , `_q(${ value } ,${ valueBinding } )` )
8394 addHandler ( el , 'change' , genAssignmentCode ( value , valueBinding ) , null , true )
8495}
8596
8697function genDefaultModel (
8798 el : ASTElement ,
8899 value : string ,
89- modifiers : ?Object
100+ modifiers : ?ASTModifiers
90101) : ?boolean {
91102 if ( process . env . NODE_ENV !== 'production' ) {
92103 if ( el . tag === 'input' && el . attrsMap . value ) {
@@ -111,12 +122,13 @@ function genDefaultModel (
111122 const needCompositionGuard = ! lazy && type !== 'range'
112123 const isNative = el . tag === 'input' || el . tag === 'textarea'
113124
114- const valueExpression = isNative
125+ let valueExpression = isNative
115126 ? `$event.target.value${ trim ? '.trim()' : '' } `
116127 : `$event`
117- let code = number || type === 'number'
118- ? genAssignmentCode ( value , `_n(${ valueExpression } )` )
119- : genAssignmentCode ( value , valueExpression )
128+ valueExpression = number || type === 'number'
129+ ? `_n(${ valueExpression } )`
130+ : valueExpression
131+ let code = genAssignmentCode ( value , valueExpression )
120132 if ( isNative && needCompositionGuard ) {
121133 code = `if($event.target.composing)return;${ code } `
122134 }
@@ -133,14 +145,20 @@ function genDefaultModel (
133145 addHandler ( el , event , code , null , true )
134146}
135147
136- function genSelect ( el : ASTElement , value : string ) {
148+ function genSelect (
149+ el : ASTElement ,
150+ value : string ,
151+ modifiers : ?ASTModifiers
152+ ) {
137153 if ( process . env . NODE_ENV !== 'production' ) {
138154 el . children . some ( checkOptionWarning )
139155 }
140156
157+ const number = modifiers && modifiers . number
141158 const assignment = `Array.prototype.filter` +
142159 `.call($event.target.options,function(o){return o.selected})` +
143- `.map(function(o){return "_value" in o ? o._value : o.value})` +
160+ `.map(function(o){var val = "_value" in o ? o._value : o.value;` +
161+ `return ${ number ? '_n(val)' : 'val' } })` +
144162 ( el . attrsMap . multiple == null ? '[0]' : '' )
145163
146164 const code = genAssignmentCode ( value , assignment )
0 commit comments