Skip to content

Commit 41d774d

Browse files
committed
perf(v-model): tweak setSelected
1 parent f40da5d commit 41d774d

File tree

1 file changed

+37
-35
lines changed
  • src/platforms/web/runtime/directives

1 file changed

+37
-35
lines changed

src/platforms/web/runtime/directives/model.js

+37-35
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,47 @@ export default {
6060
}
6161

6262
function setSelected (el, binding, vm) {
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
63+
actuallySetSelected(el, binding, vm)
64+
/* istanbul ignore if */
65+
if (isIE || isEdge) {
66+
setTimeout(() => {
67+
actuallySetSelected(el, binding, vm)
68+
}, 0)
69+
}
70+
}
71+
72+
function actuallySetSelected (el, binding, vm) {
73+
const value = binding.value
74+
const isMultiple = el.multiple
75+
if (isMultiple && !Array.isArray(value)) {
76+
process.env.NODE_ENV !== 'production' && warn(
77+
`<select multiple v-model="${binding.expression}"> ` +
78+
`expects an Array value for its binding, but got ${
79+
Object.prototype.toString.call(value).slice(8, -1)
80+
}`,
81+
vm
82+
)
83+
return
84+
}
85+
let selected, option
86+
for (let i = 0, l = el.options.length; i < l; i++) {
87+
option = el.options[i]
88+
if (isMultiple) {
89+
selected = looseIndexOf(value, getValue(option)) > -1
90+
if (option.selected !== selected) {
91+
option.selected = selected
92+
}
93+
} else {
94+
if (looseEqual(getValue(option), value)) {
95+
if (el.selectedIndex !== i) {
96+
el.selectedIndex = i
9097
}
98+
return
9199
}
92100
}
93-
if (!isMultiple) {
94-
el.selectedIndex = -1
95-
}
96101
}
97-
98-
cb()
99-
/* istanbul ignore if */
100-
if (isIE || isEdge) {
101-
setTimeout(cb, 0)
102+
if (!isMultiple) {
103+
el.selectedIndex = -1
102104
}
103105
}
104106

0 commit comments

Comments
 (0)