8000 fix: once event modifier (#752) · ashrafali46/nativescript-vue@2a1b06f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a1b06f

Browse files
authored
fix: once event modifier (nativescript-vue#752)
closes nativescript-vue#748 * fix: updateListeners from vue need an once handler Update log while here to make it clear where capture is not supported * test(samples): add sample for once directive
1 parent c4be1c5 commit 2a1b06f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

platform/nativescript/runtime/modules/events.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@ import { updateListeners } from 'core/vdom/helpers/update-listeners'
22

33
let target
44

5+
function createOnceHandler(event, handler, capture) {
6+
const _target = target // save current target element in closure
7+
return function onceHandler() {
8+
const res = handler.apply(null, arguments)
9+
if (res !== null) {
10+
remove(event, onceHandler, capture, _target)
11+
}
12+
}
13+
}
14+
515
function add(event, handler, once, capture) {
616
if (capture) {
7-
console.log('bubble phase not supported')
17+
console.log('NativeScript-Vue do not support event in bubble phase.')
818
return
919
}
1020
if (once) {
@@ -30,7 +40,7 @@ function updateDOMListeners(oldVnode, vnode) {
3040
const on = vnode.data.on || {}
3141
const oldOn = oldVnode.data.on || {}
3242
target = vnode.elm
33-
updateListeners(on, oldOn, add, remove, vnode.context)
43+
updateListeners(on, oldOn, add, remove, createOnceHandler, vnode.context)
3444
target = undefined
3545
}
3646

samples/app/748.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const Vue = require('nativescript-vue')
2+
3+
Vue.config.debug = true
4+
Vue.config.silent = false
5+
6+
new Vue({
7+
data: {
8+
foo: false
9+
},
10+
template: `
11+
<Frame>
12+
<Page>
13+
<ActionBar title="Issue #748" />
14+
15+
<StackLayout>
16+
<Button text="Only once" @tap.once="onlyOnce" />
17+
</StackLayout>
18+
</Page>
19+
</Frame>
20+
`,
21+
methods: {
22+
onlyOnce() {
23+
console.log('this log should only appear once')
24+
}
25+
}
26+
}).$start()

0 commit comments

Comments
 (0)
0