1
1
import Vue from 'vue'
2
+ import { looseEqual } from 'shared/util'
2
3
3
4
/**
4
5
* setting <select>'s value in IE9 doesn't work
@@ -8,15 +9,19 @@ function updateSelect (el, value) {
8
9
var options = el . options
9
10
var i = options . length
10
11
while ( i -- ) {
11
- /* eslint-disable eqeqeq */
12
- if ( options [ i ] . value == value ) {
13
- /* eslint-enable eqeqeq */
12
+ if ( looseEqual ( getValue ( options [ i ] ) , value ) ) {
14
13
options [ i ] . selected = true
15
14
break
16
15
}
17
16
}
18
17
}
19
18
19
+ function getValue ( option ) {
20
+ return '_value' in option
21
+ ? option . _value
22
+ : option . value || option . text
23
+ }
24
+
20
25
describe ( 'Directive v-model select' , ( ) => {
21
26
it ( 'should work' , done => {
22
27
const vm = new Vue ( {
@@ -69,6 +74,34 @@ describe('Directive v-model select', () => {
69
74
} ) . then ( done )
70
75
} )
71
76
77
+ it ( 'should work with value bindings (object loose equal)' , done => {
78
+ const vm = new Vue ( {
79
+ data : {
80
+ test : { a : 2 }
81
+ } ,
82
+ template :
83
+ '<select v-model="test">' +
84
+ '<option value="1">a</option>' +
85
+ '<option :value="{ a: 2 }">b</option>' +
86
+ '<option :value="{ a: 3 }">c</option>' +
87
+ '</select>'
88
+ } ) . $mount ( )
89
+ document . body . appendChild ( vm . $el )
90
+ expect ( vm . $el . childNodes [ 1 ] . selected ) . toBe ( true )
91
+ vm . test = { a : 3 }
92
+ waitForUpdate ( function ( ) {
93
+ expect ( vm . $el . childNodes [ 2 ] . selected ) . toBe ( true )
94
+
95
+ updateSelect ( vm . $el , '1' )
96
+ triggerEvent ( vm . $el , 'change' )
97
+ expect ( vm . test ) . toBe ( '1' )
98
+
99
+ updateSelect ( vm . $el , { a : 2 } )
100
+ triggerEvent ( vm . $el , 'change' )
101
+ expect ( vm . test ) . toEqual ( { a : 2 } )
102
+ } ) . then ( done )
103
+ } )
104
+
72
105
it ( 'should work with v-for' , done => {
73
106
const vm = new Vue ( {
74
107
data : {
0 commit comments