22
33import { isIE } from 'core/util/env'
44import { addHandler , addProp , getBindingAttr } from 'compiler/helpers'
5+ import parseModel from 'web/util/model'
56
67let warn
78
@@ -79,7 +80,7 @@ function genRadioModel (el: ASTElement, value: string) {
7980 }
8081 const valueBinding = getBindingAttr ( el , 'value' ) || 'null'
8182 addProp ( el , 'checked' , `_q(${ value } ,${ valueBinding } )` )
82- addHandler ( el , 'change' , ` ${ value } = ${ valueBinding } ` , null , true )
83+ addHandler ( el , 'change' , genAssignmentCode ( value , valueBinding ) , null , true )
8384}
8485
8586function genDefaultModel (
@@ -114,8 +115,8 @@ function genDefaultModel (
114115 ? `$event.target.value${ trim ? '.trim()' : '' } `
115116 : `$event`
116117 let code = number || type === 'number'
117- ? ` ${ value } = _n(${ valueExpression } )`
118- : ` ${ value } = ${ valueExpression } `
118+ ? genAssignmentCode ( value , ` _n(${ valueExpression } )`)
119+ : genAssignmentCode ( value , valueExpression )
119120 if ( isNative && needCompositionGuard ) {
120121 code = `if($event.target.composing)return;${ code } `
121122 }
@@ -136,10 +137,13 @@ function genSelect (el: ASTElement, value: string) {
136137 if ( process . env . NODE_ENV !== 'production' ) {
137138 el . children . some ( checkOptionWarning )
138139 }
139- const code = `${ value } =Array.prototype.filter` +
140+
141+ const assignment = `Array.prototype.filter` +
140142 `.call($event.target.options,function(o){return o.selected})` +
141143 `.map(function(o){return "_value" in o ? o._value : o.value})` +
142144 ( el . attrsMap . multiple == null ? '[0]' : '' )
145+
146+ const code = genAssignmentCode ( value , assignment )
143147 addHandler ( el , 'change' , code , null , true )
144148}
145149
@@ -156,3 +160,15 @@ function checkOptionWarning (option: any): boolean {
156160 }
157161 return false
158162}
163+
164+ function genAssignmentCode ( value : string , assignment : string ) : string {
165+ const modelRs = parseModel ( value )
166+ if ( modelRs . idx === null ) {
167+ return `${ value } =${ assignment } `
168+ } else {
169+ return `var $$exp = ${ modelRs . exp } , $$idx = ${ modelRs . idx } ;` +
170+ `if (!Array.isArray($$exp)){` +
171+ `${ value } =${ assignment } }` +
172+ `else{$$exp.splice($$idx, 1, ${ assignment } )}`
173+ }
174+ }
0 commit comments