Skip to content
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

fix #6193, change event should not be triggered if each value still matches an option #6194

Merged
merged 3 commits into from
Aug 29, 2017
Merged
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
13 changes: 12 additions & 1 deletion src/platforms/web/runtime/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ export default {
const prevOptions = el._vOptions
const curOptions = el._vOptions = [].map.call(el.options, getValue)
if (curOptions.some((o, i) => !looseEqual(o, prevOptions[i]))) {
trigger(el, 'change')
// trigger change event if
// no matching option found for at least one value
const needReset = el.multiple
? binding.value.some(v => hasNoMatchingOption(v, curOptions))
: binding.value !== binding.oldValue && hasNoMatchingOption(binding.value, curOptions)
if (needReset) {
trigger(el, 'change')
}
}
}
}
Expand Down Expand Up @@ -101,6 +108,10 @@ function setSelected (el, binding, vm) {
}
}

function hasNoMatchingOption (value, options) {
Copy link
Member

@Kingwl Kingwl Jul 24, 2017

Choose a reason for hiding this comment

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

how about Array.prototype.some or every😅

Copy link
Member Author

@jkzing jkzing Jul 24, 2017

Choose a reason for hiding this comment

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

Yes, it would be better to use [].some.
Actually I just did revert changes made in c90b140, and didn't think about that. 😅
Will do it later.

return options.every(o => !looseEqual(o, value))
}

function getValue (option) {
return '_value' in option
? option._value
Expand Down
29 changes: 29 additions & 0 deletions test/unit/features/directives/model-select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,33 @@ describe('Directive v-model select', () => {
expect(vm.test).toBe('2')
}).then(done)
})

// #6193
it('should not trigger change event when matching option can be found for each value', done => {
const spy = jasmine.createSpy()
const vm = new Vue({
data: {
options: ['1']
},
computed: {
test: {
get () {
return '1'
},
set () {
spy()
}
}
},
template:
'<select v-model="test">' +
'<option :key="opt" v-for="opt in options" :value="opt">{{ opt }}</option>' +
'</select>'
}).$mount()

vm.options = ['1', '2']
waitForUpdate(() => {
expect(spy).not.toHaveBeenCalled()
}).then(done)
})
})