From 41d774d112946f986bf0b0e3f30fd962c01ceba2 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 29 Aug 2017 17:23:00 +0200 Subject: [PATCH] perf(v-model): tweak setSelected --- src/platforms/web/runtime/directives/model.js | 72 ++++++++++--------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/src/platforms/web/runtime/directives/model.js b/src/platforms/web/runtime/directives/model.js index eee454302b..5930e2d7ba 100644 --- a/src/platforms/web/runtime/directives/model.js +++ b/src/platforms/web/runtime/directives/model.js @@ -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( - ` ` + + `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 } }