Skip to content

fix #10614: the select's v-model set to undefined in some corner case #10615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/platforms/web/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function genSelect (
`.map(function(o){var val = "_value" in o ? o._value : o.value;` +
`return ${number ? '_n(val)' : 'val'}})`

const assignment = '$event.target.multiple ? $$selectedVal : $$selectedVal[0]'
Copy link
Author

@xieranmaya xieranmaya Oct 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$$selectedVal may be an empty array, this way we should not return its first element
reserve the model's previous value is a reasonable choice

const assignment = `$event.target.multiple ? $$selectedVal : ($$selectedVal.length > 0 ? $$selectedVal[0] : ${value})`
let code = `var $$selectedVal = ${selectedVal};`
code = `${code} ${genAssignmentCode(value, assignment)}`
addHandler(el, 'change', code, null, true)
Expand Down
19 changes: 19 additions & 0 deletions test/unit/features/directives/model-select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,25 @@ describe('Directive v-model select', () => {
}).then(done)
})

it('should not set model to `undefined` when both model and options changed', (done) => {
const vm = new Vue({
data: {
test: 'a',
opts: ['a', 'b', 'c']
},
template:
'<select v-model="test">' +
'<option v-for="o in opts" :value="o">option {{ o }}</option>' +
'</select>'
}).$mount()
document.body.appendChild(vm.$el)
vm.test = '1'
vm.opts = ['2', '3', '4']
waitForUpdate(() => {
expect(vm.test).toBe('1') // should not set vm.test to `undefined` but reserves '1'
}).then(done)
})

if (!hasMultiSelectBug()) {
it('multiple', done => {
const vm = new Vue({
Expand Down