8000 fix: remove double event listeners and adjust event name handling · heywhy/nativescript-vue@3ab65c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ab65c7

Browse files
committed
fix: remove double event listeners and adjust event name handling
closes nativescript-vue#1010
1 parent f6f8748 commit 3ab65c7

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

demo/app/components/GH1010.vue

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts" setup>
2+
import { ref } from "vue";
3+
4+
const tapCount = ref(0);
5+
6+
function onTap() {
7+
tapCount.value++;
8+
console.trace("Tapped!", tapCount.value);
9+
}
10+
11+
function onLoaded(args) {
12+
console.log("STACK LOADED")
13+
}
14+
</script>
15+
16+
<template>
17+
<StackLayout @loaded="onLoaded" @LOADED="onLoaded" @lOaD="onLoaded">
18+
<Label>Tap count: {{ tapCount }}</Label>
19+
<Button @tap="(tapCount = 0)">Reset</Button>
20+
21+
<!-- correct: fires only once -->
22+
<Button @tap="onTap">Tap: Button</Button>
23+
24+
<!-- incorrect: fires twice! -->
25+
<Label @tap="onTap">Tap: Label</Label>
26+
</StackLayout>
27+
</template>
28+

demo/app/components/GH1010.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
appId: org.nativescript.demo
2+
---
3+
- launchApp
4+
- assertVisible: "Tap Count: 0"
5+
- tapOn: "Tap: Button"
6+
- assertVisible: "Tap Count: 1"
7+
- tapOn: "Tap: Label"
8+
- assertVisible: "Tap Count: 2"

src/dom/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ export class NSVElement extends NSVNode {
163163
}
164164

165165
this.nativeView.addEventListener(event, handler);
166-
// temporary, @loaded becomes `onLoaded` -> `Loaded`
167-
this.nativeView.addEventListener(event.toLowerCase(), handler);
168166
}
169167

170168
removeEventListener(event: string, handler?: any) {

src/renderer/modules/events.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,16 @@ function parseName(name: string): [string, EventListenerOptions | undefined] {
6969
options;
7070
}
7171
}
72+
73+
// todo: optimise if necessary
74+
// this isn't technically perfect,
75+
// since if the event name was UPPER then we'll convert it to uPPER
76+
// but for the majority of cases, this should be the right thing to do.
77+
name = name.slice(name.startsWith("on:") ? 3 : 2);
78+
name = name.charAt(0).toLowerCase() + name.slice(1);
79+
7280
// return event name by removing on prefix. eg. onitemTap -> itemTap
73-
return [name.slice(2), options];
81+
return [name, options];
7482
// return [hyphenate(name.slice(2)), options]
7583
}
7684

0 commit comments

Comments
 (0)
0