Skip to content

Commit

Permalink
adjust v-model on component sync mechanism (fix vuejs#3179)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 29, 2016
1 parent 1b60a88 commit 6cf1929
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
12 changes: 1 addition & 11 deletions examples/select2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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',
Expand Down
9 changes: 6 additions & 3 deletions src/platforms/web/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/features/directives/model-component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 6cf1929

Please sign in to comment.