Skip to content

Commit aab74bd

Browse files
committed
handle v-model select initial value edge cases in IE (fix vuejs#2983)
1 parent ab67578 commit aab74bd

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/directives/public/model/select.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { isArray, toNumber, looseEqual } from '../../../util/index'
1+
import {
2+
inDoc,
3+
isArray,
4+
toNumber,
5+
looseEqual,
6+
nextTick
7+
} from '../../../util/index'
28

39
export default {
410

@@ -39,11 +45,16 @@ export default {
3945
// selectedIndex with value -1 to 0 when the element
4046
// is appended to a new parent, therefore we have to
4147
// force a DOM update whenever that happens...
42-
this.vm.$on('hook:attached', this.forceUpdate)
48+
this.vm.$on('hook:attached', () => {
49+
nextTick(this.forceUpdate)
50+
})
4351
},
4452

4553
update (value) {
4654
var el = this.el
55+
if (!inDoc(el)) {
56+
return nextTick(this.forceUpdate)
57+
}
4758
el.selectedIndex = -1
4859
var multi = this.multiple && isArray(value)
4960
var options = el.options

src/util/env.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const devtools = inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__
1313

1414
// UA sniffing for working around browser-specific quirks
1515
const UA = inBrowser && window.navigator.userAgent.toLowerCase()
16+
export const isIE = UA && UA.indexOf('trident') > 0
1617
export const isIE9 = UA && UA.indexOf('msie 9.0') > 0
1718
export const isAndroid = UA && UA.indexOf('android') > 0
1819
export const isIos = UA && /(iphone|ipad|ipod|ios)/i.test(UA)

test/unit/specs/directives/public/model_spec.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ describe('v-model', function () {
235235
})
236236
})
237237

238-
it('select persist non-selected on append', function () {
238+
it('select persist non-selected on append', function (done) {
239239
var vm = new Vue({
240240
el: el,
241241
data: {
@@ -249,12 +249,17 @@ describe('v-model', function () {
249249
'<option>c</option>' +
250250
'</select>'
251251
})
252-
expect(vm.$el.value).toBe('')
253-
expect(vm.$el.selectedIndex).toBe(-1)
254-
vm.$remove()
255-
vm.$appendTo(document.body)
256-
expect(vm.$el.value).toBe('')
257-
expect(vm.$el.selectedIndex).toBe(-1)
252+
_.nextTick(function () {
253+
expect(vm.$el.value).toBe('')
254+
expect(vm.$el.selectedIndex).toBe(-1)
255+
vm.$remove()
256+
vm.$appendTo(document.body)
257+
_.nextTick(function () {
258+
expect(vm.$el.value).toBe('')
259+
expect(vm.$el.selectedIndex).toBe(-1)
260+
done()
261+
})
262+
})
258263
})
259264

260265
it('select template default value', function () {

0 commit comments

Comments
 (0)