Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export function genAssignmentCode (
): string {
const modelRs = parseModel(value)
if (modelRs.idx === null) {
return `if (typeof (${assignment}) === 'object' && (${assignment}).type === 'input') { ${value}=(${assignment}).target.value } else { ${value}=${assignment} }`
return `if (typeof (${assignment}) === 'object' && (${assignment}).type === 'input') {` +
`${value}=(${assignment}).target.value || (${assignment}).target.attr.value` +
`} else { ${value}=${assignment} }`
} else {
return `$set(${modelRs.exp}, ${modelRs.idx}, ${assignment})`
}
Expand Down
15 changes: 10 additions & 5 deletions test/weex/compiler/v-model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ describe('compile v-model', () => {
const { render, staticRenderFns, errors } = compile(`<div><input v-model="x" /></div>`)
expect(render).not.toBeUndefined()
expect(render).toMatch(strToRegExp(`attrs:{"value":(x)}`))
expect(render).toMatch(strToRegExp(`on:{"input":function($event){x=$event.target.attr.value}}`))
expect(render).toMatch(strToRegExp(`"input":function($event)`))
expect(render).toMatch(strToRegExp(`x=$event.target.attr.value`))
expect(staticRenderFns).toEqual([])
expect(errors).toEqual([])
})

it('should compile other component with whole $event as the value', () => {
const { render, staticRenderFns, errors } = compile(`<div><foo v-model="x" /></div>`)
expect(render).not.toBeUndefined()
expect(render).toMatch(strToRegExp(`model:{value:(x),callback:function ($$v) {x=$$v},expression:"x"}`))
expect(render).toMatch(strToRegExp(`callback:function`))
expect(render).toMatch(strToRegExp(`x=$$v`))
expect(render).toMatch(strToRegExp(`expression:"x"`))
expect(staticRenderFns).toEqual([])
expect(errors).toEqual([])
})
Expand All @@ -23,7 +26,8 @@ describe('compile v-model', () => {
const { render, staticRenderFns, errors } = compile(`<div><input v-model.trim="x" /></div>`)
expect(render).not.toBeUndefined()
expect(render).toMatch(strToRegExp(`attrs:{"value":(x)}`))
expect(render).toMatch(strToRegExp(`on:{"input":function($event){x=$event.target.attr.value.trim()}}`))
expect(render).toMatch(strToRegExp(`"input":function($event)`))
expect(render).toMatch(strToRegExp(`x=$event.target.attr.value.trim()`))
expect(staticRenderFns).toEqual([])
expect(errors).toEqual([])
})
Expand All @@ -33,8 +37,9 @@ describe('compile v-model', () => {
expect(render).not.toBeUndefined()
expect(render).toMatch(strToRegExp(`attrs:{"value":(x)}`))
expect(render).toMatch(strToRegExp(`attrs:{"value":(y)}`))
expect(render).toMatch(strToRegExp(`on:{"change":function($event){x=$event.target.attr.value.trim()}}`))
expect(render).toMatch(strToRegExp(`on:{"change":function($event){y=$event.target.attr.value.trim()}}`))
expect(render).toMatch(strToRegExp(`"change":function($event)`))
expect(render).toMatch(strToRegExp(`x=$event.target.attr.value.trim()`))
expect(render).toMatch(strToRegExp(`y=$event.target.attr.value.trim()`))
expect(staticRenderFns).toEqual([])
expect(errors).toEqual([])
})
Expand Down
73 changes: 73 additions & 0 deletions test/weex/runtime/events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,77 @@ describe('generate events', () => {
})
})
})

it('should work with v-model', (done) => {
const { render, staticRenderFns } = compileAndStringify(`
<div>
<text>{{ msg }}</text>
<input v-model="msg">
</div>
`)
const instance = createInstance(runtime, `
new Vue({
el: 'body',
data: {
msg: 'hello'
},
render: ${render},
staticRenderFns: ${staticRenderFns}
})
`)
setTimeout(() => {
const $text = instance.doc.body.children[0]
const $input = instance.doc.body.children[1]
expect($text.attr.value).toEqual('hello')
expect($input.attr.value).toEqual('hello')
$input.attr.value = 'world'
instance.$fireEvent($input.ref, 'input', {}, { attrs: $input.attr })
setTimeout(() => {
expect($input.attr.value).toEqual('world')
expect($text.attr.value).toEqual('world')
done()
}, 0)
}, 0)
})

it('should work with v-bind="$attrs" plus v-on="$listeners"', (done) => {
const childTemplate = compileAndStringify(`
<input v-bind="$attrs" v-on="$listeners">
`)
const { render, staticRenderFns } = compileAndStringify(`
<div>
<text>{{ msg }}</text>
<child v-model="msg"></child>
</div>
`)
const instance = createInstance(runtime, `
new Vue({
el: 'body',
data: {
msg: 'hello'
},
render: ${render},
staticRenderFns: ${staticRenderFns},
components: {
child: {
render: ${childTemplate.render},
staticRenderFns: ${childTemplate.staticRenderFns}
}
}
})
`)
setTimeout(() => {
const $text = instance.doc.body.children[0]
const $input = instance.doc.body.children[1]
expect($text.attr.value).toEqual('hello')
expect($input.attr.value).toEqual('hello')
$input.attr.value = 'world'
instance.$fireEvent($input.ref, 'input', {}, { attrs: $input.attr })
setTimeout(() => {
expect($input.attr.value).toEqual('world')
expect($text.attr.value).toEqual('world')
done()
}, 0)
}, 0)
})
})