diff --git a/src/platforms/web/runtime/directives/model.js b/src/platforms/web/runtime/directives/model.js index ee355978421..72da9f45fe2 100644 --- a/src/platforms/web/runtime/directives/model.js +++ b/src/platforms/web/runtime/directives/model.js @@ -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') + } } } } @@ -101,6 +108,10 @@ function setSelected (el, binding, vm) { } } +function hasNoMatchingOption (value, options) { + return options.every(o => !looseEqual(o, value)) +} + function getValue (option) { return '_value' in option ? option._value diff --git a/test/unit/features/directives/model-select.spec.js b/test/unit/features/directives/model-select.spec.js index 2f7e20eac2c..629e5a2f440 100644 --- a/test/unit/features/directives/model-select.spec.js +++ b/test/unit/features/directives/model-select.spec.js @@ -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: + '' + }).$mount() + + vm.options = ['1', '2'] + waitForUpdate(() => { + expect(spy).not.toHaveBeenCalled() + }).then(done) + }) })