diff --git a/src/platforms/web/compiler/directives/model.js b/src/platforms/web/compiler/directives/model.js index 37da7f9f6c9..8f5951eac56 100644 --- a/src/platforms/web/compiler/directives/model.js +++ b/src/platforms/web/compiler/directives/model.js @@ -93,7 +93,7 @@ function genCheckboxModel ( 'if(Array.isArray($$a)){' + `var $$v=${number ? '_n(' + valueBinding + ')' : valueBinding},` + '$$i=_i($$a,$$v);' + - `if($$c){$$i<0&&(${value}=$$a.concat($$v))}` + + `if($$el.checked){$$i<0&&(${value}=$$a.concat($$v))}` + `else{$$i>-1&&(${value}=$$a.slice(0,$$i).concat($$a.slice($$i+1)))}` + `}else{${genAssignmentCode(value, '$$c')}}`, null, true diff --git a/test/unit/features/directives/model-checkbox.spec.js b/test/unit/features/directives/model-checkbox.spec.js index 09d98773b3a..2c296ccbf5a 100644 --- a/test/unit/features/directives/model-checkbox.spec.js +++ b/test/unit/features/directives/model-checkbox.spec.js @@ -77,6 +77,34 @@ describe('Directive v-model checkbox', () => { }).then(done) }) + it('bind to Array value ignores false-value', done => { + const vm = new Vue({ + data: { + test: ['1'] + }, + template: ` +
+ + +
+ ` + }).$mount() + document.body.appendChild(vm.$el) + expect(vm.$el.children[0].checked).toBe(true) + expect(vm.$el.children[1].checked).toBe(false) + vm.$el.children[0].click() + expect(vm.test.length).toBe(0) + vm.$el.children[1].click() + expect(vm.test).toEqual(['2']) + vm.$el.children[0].click() + expect(vm.test).toEqual(['2', '1']) + vm.test = ['1'] + waitForUpdate(() => { + expect(vm.$el.children[0].checked).toBe(true) + expect(vm.$el.children[1].checked).toBe(false) + }).then(done) + }) + it('bind to Array value with value bindings', done => { const vm = new Vue({ data: {