Skip to content

Commit ef57aa2

Browse files
committed
update weex events module
1 parent 05c769b commit ef57aa2

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/platforms/weex/runtime/modules/events.js

+38-9
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,50 @@
22

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

5+
let target: any
6+
7+
function add (
8+
event: string,
9+
handler: Function,
10+
once: boolean,
11+
capture: boolean
12+
) {
13+
if (capture) {
14+
console.log('Weex do not support event in bubble phase.')
15+
return
16+
}
17+
if (once) {
18+
const oldHandler = handler
19+
const _target = target // save current target element in closure
20+
handler = function (ev) {
21+
const res = arguments.length === 1
22+
? oldHandler(ev)
23+
: oldHandler.apply(null, arguments)
24+
if (res !== null) {
25+
remove(event, null, null, _target)
26+
}
27+
}
28+
}
29+
target.addEvent(event, handler)
30+
}
31+
32+
function remove (
33+
event: string,
34+
handler: any,
35+
capture: any,
36+
_target?: any
37+
) {
38+
(_target || target).removeEvent(event)
39+
}
40+
541
function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
642
if (!oldVnode.data.on && !vnode.data.on) {
743
return
844
}
945
const on = vnode.data.on || {}
1046
const oldOn = oldVnode.data.on || {}
11-
updateListeners(on, oldOn, (event, handler, capture) => {
12-
if (capture) {
13-
console.log('Weex do not support event in bubble phase.')
14-
return
15-
}
16-
vnode.elm.addEvent(event, handler.bind(vnode.context))
17-
}, (event) => {
18-
vnode.elm.removeEvent(event)
19-
}, vnode.context)
47+
target = vnode.elm
48+
updateListeners(on, oldOn, add, remove, vnode.context)
2049
}
2150

2251
export default {

0 commit comments

Comments
 (0)