Explorar el Código

feat: v-bind.sync also listens for kebab-case update event (#8297)

 fix #6428
Divya hace 7 años
padre
commit
3fca52792e
Se han modificado 2 ficheros con 32 adiciones y 3 borrados
  1. 11 3
      src/compiler/parser/index.js
  2. 21 0
      test/unit/features/directives/bind.spec.js

+ 11 - 3
src/compiler/parser/index.js

@@ -5,7 +5,7 @@ import { parseHTML } from './html-parser'
 import { parseText } from './text-parser'
 import { parseText } from './text-parser'
 import { parseFilters } from './filter-parser'
 import { parseFilters } from './filter-parser'
 import { genAssignmentCode } from '../directives/model'
 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 { isIE, isEdge, isServerRendering } from 'core/util/env'
 
 
 import {
 import {
@@ -524,7 +524,7 @@ function processComponent (el) {
 
 
 function processAttrs (el) {
 function processAttrs (el) {
   const list = el.attrsList
   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++) {
   for (i = 0, l = list.length; i < l; i++) {
     name = rawName = list[i].name
     name = rawName = list[i].name
     value = list[i].value
     value = list[i].value
@@ -558,11 +558,19 @@ function processAttrs (el) {
             name = camelize(name)
             name = camelize(name)
           }
           }
           if (modifiers.sync) {
           if (modifiers.sync) {
+            syncGen = genAssignmentCode(value, `$event`)
             addHandler(
             addHandler(
               el,
               el,
               `update:${camelize(name)}`,
               `update:${camelize(name)}`,
-              genAssignmentCode(value, `$event`)
+              syncGen
             )
             )
+            if (hyphenate(name) !== camelize(name)) {
+              addHandler(
+                el,
+                `update:${hyphenate(name)}`,
+                syncGen
+              )
+            }
           }
           }
         }
         }
         if (isProp || (
         if (isProp || (

+ 21 - 0
test/unit/features/directives/bind.spec.js

@@ -166,6 +166,27 @@ describe('Directive v-bind', () => {
     }).then(done)
     }).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 => {
   it('bind object', done => {
     const vm = new Vue({
     const vm = new Vue({
       template: '<input v-bind="test">',
       template: '<input v-bind="test">',