Skip to content

Commit

Permalink
feat: v-bind.sync also listens for kebab-case update event (#8297)
Browse files Browse the repository at this point in the history
 fix #6428
  • Loading branch information
shortdiv authored and yyx990803 committed Dec 21, 2018
1 parent 0e4e45e commit 3fca527
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parseHTML } from './html-parser'
import { parseText } from './text-parser'
import { parseFilters } from './filter-parser'
import { genAssignmentCode } from '../directives/model'
import { extend, cached, no, camelize } from 'shared/util'
import { extend, cached, no, camelize, hyphenate } from 'shared/util'
import { isIE, isEdge, isServerRendering } from 'core/util/env'

import {
Expand Down Expand Up @@ -524,7 +524,7 @@ function processComponent (el) {

function processAttrs (el) {
const list = el.attrsList
let i, l, name, rawName, value, modifiers, isProp
let i, l, name, rawName, value, modifiers, isProp, syncGen
for (i = 0, l = list.length; i < l; i++) {
name = rawName = list[i].name
value = list[i].value
Expand Down Expand Up @@ -558,11 +558,19 @@ function processAttrs (el) {
name = camelize(name)
}
if (modifiers.sync) {
syncGen = genAssignmentCode(value, `$event`)
addHandler(
el,
`update:${camelize(name)}`,
genAssignmentCode(value, `$event`)
syncGen
)
if (hyphenate(name) !== camelize(name)) {
addHandler(
el,
`update:${hyphenate(name)}`,
syncGen
)
}
}
}
if (isProp || (
Expand Down
21 changes: 21 additions & 0 deletions test/unit/features/directives/bind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,27 @@ describe('Directive v-bind', () => {
}).then(done)
})

it('.sync modifier with kebab case event', done => {
const vm = new Vue({
template: `<test :foo-bar.sync="bar"/>`,
data: {
bar: 1
},
components: {
test: {
props: ['fooBar'],
template: `<div @click="$emit('update:foo-bar', 2)">{{ fooBar }}</div>`
}
}
}).$mount()

expect(vm.$el.textContent).toBe('1')
triggerEvent(vm.$el, 'click')
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('2')
}).then(done)
})

it('bind object', done => {
const vm = new Vue({
template: '<input v-bind="test">',
Expand Down

0 comments on commit 3fca527

Please sign in to comment.