Skip to content

Commit

Permalink
perf(v-model): tweak setSelected
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 29, 2017
1 parent f40da5d commit 41d774d
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions src/platforms/web/runtime/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,47 @@ export default {
}

function setSelected (el, binding, vm) {
const cb = () => {
const value = binding.value
const isMultiple = el.multiple
if (isMultiple && !Array.isArray(value)) {
process.env.NODE_ENV !== 'production' && warn(
`<select multiple v-model="${binding.expression}"> ` +
`expects an Array value for its binding, but got ${
Object.prototype.toString.call(value).slice(8, -1)
}`,
vm
)
return
}
let selected, option
for (let i = 0, l = el.options.length; i < l; i++) {
option = el.options[i]
if (isMultiple) {
selected = looseIndexOf(value, getValue(option)) > -1
if (option.selected !== selected) {
option.selected = selected
}
} else {
if (looseEqual(getValue(option), value)) {
if (el.selectedIndex !== i) {
el.selectedIndex = i
}
return
actuallySetSelected(el, binding, vm)
/* istanbul ignore if */
if (isIE || isEdge) {
setTimeout(() => {
actuallySetSelected(el, binding, vm)
}, 0)
}
}

function actuallySetSelected (el, binding, vm) {
const value = binding.value
const isMultiple = el.multiple
if (isMultiple && !Array.isArray(value)) {
process.env.NODE_ENV !== 'production' && warn(
`<select multiple v-model="${binding.expression}"> ` +
`expects an Array value for its binding, but got ${
Object.prototype.toString.call(value).slice(8, -1)
}`,
vm
)
return
}
let selected, option
for (let i = 0, l = el.options.length; i < l; i++) {
option = el.options[i]
if (isMultiple) {
selected = looseIndexOf(value, getValue(option)) > -1
if (option.selected !== selected) {
option.selected = selected
}
} else {
if (looseEqual(getValue(option), value)) {
if (el.selectedIndex !== i) {
el.selectedIndex = i
}
return
}
}
if (!isMultiple) {
el.selectedIndex = -1
}
}

cb()
/* istanbul ignore if */
if (isIE || isEdge) {
setTimeout(cb, 0)
if (!isMultiple) {
el.selectedIndex = -1
}
}

Expand Down

0 comments on commit 41d774d

Please sign in to comment.