-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Event bubbling #149
Comments
Programmatic events don't bubble the way DOM events do, no. I know you're asking for a simple yes/no answer as to whether/how this is possible, but bear with me while I ramble for a bit for the sake of debate and anyone else who is wondering about the rationale for this. It could be implemented like so: // in the generated code
component.on( '*', function ( eventName, event ) {
this.fire( eventName, event );
});
// in the `fire` method
this.fire = function fire ( eventName, data ) {
var universalHandlers = callbacks['*'];
for ( var i = 0; i < handlers.length; i += 1 ) {
universalHandlers[i].call( this, eventName, data );
}
var handlers = eventName in callbacks && callbacks[ eventName ].slice();
if ( !handlers ) return;
for ( var i = 0; i < handlers.length; i += 1 ) {
handlers[i].call( this, data );
}
}; (There's some nuance around callback order etc that I'm glossing over, but you get the idea.) The trouble is that when you add component event bubbling, it becomes harder to understand how a single component will behave just by looking at it, if it has nested components – you need to be well acquainted with the entire subtree before you can feel confident. Your app as a whole becomes less predictable. (This isn't academic – Ractive, Svelte's forerunner, does allow event bubbling and it's been a source of bugs and confusion on several of my projects.) Beyond that, if you have event bubbling then you need to have a way of preventing bubbling, analogous to All that extra runtime code and machinery for a feature that's occasionally convenient, but which is just as often a source of problems! So I'm inclined not to go down the bubbling route. Which leads to the obvious question: how to make this work? You could manually propagate events like so: <Link on:navigate='navigate(event)'></Link> But that is a little verbose. So I wonder if there's a case for some dedicated syntax for event propagation, which could be as simple as <Link on:navigate></Link> (and has precedent in the form of the FYI: note that we're discussing removing |
Hey, thanks, I figured that it would have something to do with how easy it is to view what a component does - however in some cases, that's desirable in order to encapsulate concerns. For example, if I were to release a routing library, I wouldn't really care about where the This leads me to one of two options:
Thanks for the note on |
I have no idea if this is desirable but you can get event bubbeling by using DOM Events. I created a little (very simple) routing example here The main thing that makes this work is <a href="#" on:click="navigate(event)" ref:link>{{yield}}</a>
<script>
export default {
methods: {
navigate(event) {
event.preventDefault();
this.refs.link.dispatchEvent(
new CustomEvent('navigate', {
bubbles: true,
detail: {
pathname: this.get('to') // <Link to="/home">Go Home</Link>
}
})
);
}
}
}
</script> And the Router is using |
@danpantry You closed the issue today. What was your final solution? |
@Ryuno-Ki I didn't have one. I ended up not using Svelte as it didn't have the tooling support I needed and it was too much effort to get it working compared to something like React, especially since the file size of the application I was working on was not an issue. I was just going through open issues/PRs/old repo forks and clearing them out. |
Consider the following components:
Link.html
LandingPage.html
Router.html
Main.html
Given the above, I was hoping that
Link
would propagate itsnavigate
event up to theRouter
component, which would catch it and then act on that event. Am I doing something FUBAR here or is there an alternative pattern for orthogonal concerns like Routing? I would like to be able to just use a<Link>
component without the rest of the application having to be concerned about "how" routing is done and have it "just work".Currently, clicking the
plz click
button will not propagate the event up to Router, so nothing happens. The only other alternative I can see is by manipulating the history from theLink
component, which sort-of works but I see that being problematic in terms of re-use.The text was updated successfully, but these errors were encountered: