Skip to content

Commit

Permalink
fix(v-model): Allow using array value with array v-model in checkboxes (
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmessing authored and yyx990803 committed Sep 1, 2017
1 parent d77b953 commit d6e6f1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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 @@ -93,7 +93,7 @@ function genCheckboxModel (
'if(Array.isArray($$a)){' +
`var $$v=${number ? '_n(' + valueBinding + ')' : valueBinding},` +
'$$i=_i($$a,$$v);' +
`if($$el.checked){$$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
Expand Down
28 changes: 28 additions & 0 deletions test/unit/features/directives/model-checkbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,34 @@ describe('Directive v-model checkbox', () => {
}).then(done)
})

it('bind to Array value with array value bindings (object loose equal)', done => {
const vm = new Vue({
data: {
test: [{ a: 1 }]
},
template: `
<div>
<input type="checkbox" v-model="test" :value="{ a: 1 }">
<input type="checkbox" v-model="test" :value="[2]">
</div>
`
}).$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], { a: 1 }])
vm.test = [{ a: 1 }]
waitForUpdate(() => {
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
}).then(done)
})

it('.number modifier', () => {
const vm = new Vue({
data: {
Expand Down

0 comments on commit d6e6f1d

Please sign in to comment.