@@ -22,14 +22,7 @@ if (isIE9) {
2222export default {
2323 inserted ( el , binding , vnode ) {
2424 if ( vnode . tag === 'select' ) {
25- const cb = ( ) => {
26- setSelected ( el , binding , vnode . context )
27- }
28- cb ( )
29- /* istanbul ignore if */
30- if ( isIE || isEdge ) {
31- setTimeout ( cb , 0 )
32- }
25+ setSelected ( el , binding , vnode . context )
3326 el . _vOptions = [ ] . map . call ( el . options , getValue )
3427 } else if ( vnode . tag === 'textarea' || isTextInputType ( el . type ) ) {
3528 el . _vModifiers = binding . modifiers
@@ -67,37 +60,45 @@ export default {
6760}
6861
6962function setSelected ( el , binding , vm ) {
70- const value = binding . value
71- const isMultiple = el . multiple
72- if ( isMultiple && ! Array . isArray ( value ) ) {
73- process . env . NODE_ENV !== 'production' && warn (
74- `<select multiple v-model="${ binding . expression } "> ` +
75- `expects an Array value for its binding, but got ${
76- Object . prototype . toString . call ( value ) . slice ( 8 , - 1 )
77- } `,
78- vm
79- )
80- return
81- }
82- let selected , option
83- for ( let i = 0 , l = el . options . length ; i < l ; i ++ ) {
84- option = el . options [ i ]
85- if ( isMultiple ) {
86- selected = looseIndexOf ( value , getValue ( option ) ) > - 1
87- if ( option . selected !== selected ) {
88- option . selected = selected
89- }
90- } else {
91- if ( looseEqual ( getValue ( option ) , value ) ) {
92- if ( el . selectedIndex !== i ) {
93- el . selectedIndex = i
63+ const cb = ( ) => {
64+ const value = binding . value
65+ const isMultiple = el . multiple
66+ if ( isMultiple && ! Array . isArray ( value ) ) {
67+ process . env . NODE_ENV !== 'production' && warn (
68+ `<select multiple v-model="${ binding . expression } "> ` +
69+ `expects an Array value for its binding, but got ${
70+ Object . prototype . toString . call ( value ) . slice ( 8 , - 1 )
71+ } `,
72+ vm
73+ )
74+ return
75+ }
76+ let selected , option
77+ for ( let i = 0 , l = el . options . length ; i < l ; i ++ ) {
78+ option = el . options [ i ]
79+ if ( isMultiple ) {
80+ selected = looseIndexOf ( value , getValue ( option ) ) > - 1
81+ if ( option . selected !== selected ) {
82+ option . selected = selected
83+ }
84+ } else {
85+ if ( looseEqual ( getValue ( option ) , value ) ) {
86+ if ( el . selectedIndex !== i ) {
87+ el . selectedIndex = i
88+ }
89+ return
9490 }
95- return
9691 }
9792 }
93+ if ( ! isMultiple ) {
94+ el . selectedIndex = - 1
95+ }
9896 }
99- if ( ! isMultiple ) {
100- el . selectedIndex = - 1
97+
98+ cb ( )
99+ /* istanbul ignore if */
100+ if ( isIE || isEdge ) {
101+ setTimeout ( cb , 0 )
101102 }
102103}
103104
0 commit comments