Skip to content

Commit

Permalink
update weex events module
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 14, 2017
1 parent 05c769b commit ef57aa2
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/platforms/weex/runtime/modules/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,50 @@

import { updateListeners } from 'core/vdom/helpers/update-listeners'

let target: any

function add (
event: string,
handler: Function,
once: boolean,
capture: boolean
) {
if (capture) {
console.log('Weex do not support event in bubble phase.')
return
}
if (once) {
const oldHandler = handler
const _target = target // save current target element in closure
handler = function (ev) {
const res = arguments.length === 1
? oldHandler(ev)
: oldHandler.apply(null, arguments)
if (res !== null) {
remove(event, null, null, _target)
}
}
}
target.addEvent(event, handler)
}

function remove (
event: string,
handler: any,
capture: any,
_target?: any
) {
(_target || target).removeEvent(event)
}

function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
if (!oldVnode.data.on && !vnode.data.on) {
return
}
const on = vnode.data.on || {}
const oldOn = oldVnode.data.on || {}
updateListeners(on, oldOn, (event, handler, capture) => {
if (capture) {
console.log('Weex do not support event in bubble phase.')
return
}
vnode.elm.addEvent(event, handler.bind(vnode.context))
}, (event) => {
vnode.elm.removeEvent(event)
}, vnode.context)
target = vnode.elm
updateListeners(on, oldOn, add, remove, vnode.context)
}

export default {
Expand Down

0 comments on commit ef57aa2

Please sign in to comment.