diff --git a/examples/select2/index.html b/examples/select2/index.html index 1f5d2a62..8cd339ea 100644 --- a/examples/select2/index.html +++ b/examples/select2/index.html @@ -48,7 +48,7 @@ .select2({ data: this.options }) // emit event on change. .on('change', function () { - vm.$emit('input', mockEvent(this.value)) + vm.$emit('input', this.value) }) }, watch: { @@ -66,16 +66,6 @@ } }) - // mock an event because the v-model binding expects - // event.target.value - function mockEvent (value) { - return { - target: { - value: value - } - } - } - var vm = new Vue({ el: '#el', template: '#demo-template', diff --git a/src/platforms/web/compiler/directives/model.js b/src/platforms/web/compiler/directives/model.js index 493a81bb..7f31428c 100644 --- a/src/platforms/web/compiler/directives/model.js +++ b/src/platforms/web/compiler/directives/model.js @@ -98,15 +98,18 @@ function genDefaultModel ( const { lazy, number, trim } = modifiers || {} const event = lazy ? 'change' : 'input' const needCompositionGuard = !lazy && type !== 'range' + const isNative = el.tag === 'input' || el.tag === 'textarea' - const valueExpression = `$event.target.value${trim ? '.trim()' : ''}` + const valueExpression = isNative + ? `$event.target.value${trim ? '.trim()' : ''}` + : `$event` let code = number || type === 'number' ? `${value}=_n(${valueExpression})` : `${value}=${valueExpression}` - if (needCompositionGuard) { + if (isNative && needCompositionGuard) { code = `if($event.target.composing)return;${code}` } - addProp(el, 'value', `_s(${value})`) + addProp(el, 'value', isNative ? `_s(${value})` : `(${value})`) addHandler(el, event, code) if (needCompositionGuard) { // need runtime directive code to help with composition events diff --git a/test/unit/features/directives/model-component.spec.js b/test/unit/features/directives/model-component.spec.js index 0c4cf5ff..1739e780 100644 --- a/test/unit/features/directives/model-component.spec.js +++ b/test/unit/features/directives/model-component.spec.js @@ -21,7 +21,7 @@ describe('Directive v-model component', () => { methods: { onInput (e) { // something validate ... - this.$emit('input', e) + this.$emit('input', e.target.value) } }, mounted () {