You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Svelte executes events registered in `component.$$.callbacks` when an event is triggered in a child component. In the example above, `component.$$.callbacks` is as follows:
112
+
```js
113
+
component.$$.callbacks= {
114
+
click: () =>console.log('clicked!')
115
+
}
116
+
```
117
+
118
+
This preprocessor adds a process to listen for events registered in `component.$$.callbacks` for elements with `on:*`. After preprocessing, Child.svelte looks like this:
119
+
```svelte
120
+
<!-- Child.svelte -->
121
+
<script>
122
+
import { boundElements, registerDelegatedEvents } from 'svelte-preprocess-delegate-events/runtime';
123
+
import { get_current_component } from 'svelte/internal';
NOTE: The reason for binding `<button>` to `button.bounds` instead of binding it to the `button` variable is to support cases where multiple elements exist, such as `<button>` in a `{#each}` block.
132
+
133
+
In this way, only events that are being listened to by the parent component are listened to, thus providing a mechanism with no performance overhead.
134
+
135
+
## Component
136
+
137
+
Component uses a different mechanism than Element. Consider the following simple example:
If you are using `on:*` in `Child.svelte`, you need to forward all events from `GrandChild` to `Parent`. However, `Child` does not know what events are coming from `GrandChild`, so you need to do something. Specifically, when `GrandChild` triggers an event, it will refer to `component.$$.callbacks` to run its event handlers. By proxying `component.$$.callbacks`, you will know which events have been forwarded. Forwarded events can be communicated to the parent component so that the `Parent` component can handle the event.
156
+
157
+
After preprocessing, it looks like this:
158
+
```svelte
159
+
<!-- Child.svelte -->
160
+
<script>
161
+
import { boundComponents, proxyCallbacks } from 'svelte-preprocess-delegate-events/runtime';
162
+
import { get_current_component } from 'svelte/internal';
`on:*` doesn't support event handling because I couldn't find useful usecase.
94
-
If you have a useful usecase, please create a new issue.
175
+
`on:*` does not support specifying event handlers directly because a useful use case could not be found. If you have a useful use case, please create a new issue.
95
176
96
177
```svelte
97
178
<script>
@@ -101,9 +182,9 @@ If you have a useful usecase, please create a new issue.
101
182
}
102
183
</script>
103
184
104
-
<!-- Specifying event handler does not support -->
185
+
<!-- Specifying event handler directly is not supported -->
105
186
<input on:*="{handleEvent}" />
106
187
107
-
<!-- Specifying event handler does not support -->
188
+
<!-- Specifying event handler directly is not supported -->
0 commit comments