Skip to content
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

programmatic routing #1101

Closed
drdozer opened this issue Feb 18, 2020 · 6 comments · Fixed by #1791
Closed

programmatic routing #1101

drdozer opened this issue Feb 18, 2020 · 6 comments · Fixed by #1791
Labels
A-yew-router Area: The yew-router crate feature-request A feature request

Comments

@drdozer
Copy link

drdozer commented Feb 18, 2020

I ended up with code like this within callbacks:

RouteAgent::dispatcher().send(RouteRequest::ChangeRoute(Route { route: "/".to_owned(), state: () }));

It works, but it feels like a lot of boilerplate. Would it be possible to hide this all behind an API something like:

Router::changeRoute("/", ());

I'm not sure if it should live on Router. But some statically accessible thing that lets us programmatically change route without needing to know all the innards. We probably need one static method for each of the RouteRequest verbs.

@hgzimmerman
Copy link
Member

I'm not a huge fan of outright supporting unstructured route setting more than already exists, but I am a big fan of improving the ergonomics of setting routes.

I might explore in the future having a method that exists on Switch so you can set the route without fiddling with creating dispatchers and sending the proper message type.

For example:

let new_route = AppRoute::SomeVariant{user_id: 4};
new_route.set_route_via_agent();

let new_route = AppRoute::SomeVariant{user_id: 5};
new_route.replace_route_via_agent();

@drdozer
Copy link
Author

drdozer commented Feb 25, 2020

Being able to programmatically "go to" an instance of your application switch would be just fine for me, and it feels much more ergonomic. I don't think we need the via agent suffixes though.

@hgzimmerman
Copy link
Member

I do think there needs to be some indicator through which it communicates the means through which the route change happens, otherwise it appears to be too "magical". I agree that via_agent is sort of bad.

Maybe instead:

RouteAgentDispatcher::set_route(AppRoute::SomeVariant);
RouteAgentDispatcher::replace_route(AppRoute::SomeVariant);

A little more verbose, but gets the point across much more cleanly.

Thoughts?

@jstarry jstarry transferred this issue from yewstack/yew_router Apr 20, 2020
@jstarry jstarry added feature-request A feature request A-yew-router Area: The yew-router crate labels Apr 20, 2020
@siku2
Copy link
Member

siku2 commented Jul 21, 2020

I would like to continue the discussion on this as there have been numerous users with the same problem.
A lot of users are confused by the RouteService::set_route method which changes the URL in the browser but doesn't update the router.
I believe this is partially a documentation issue but also an unfortunate name choice for the method. The docstring does state "Sets the browser's url bar [...]" but I assume a lot of users don't read the documentation first because the name is such a false friend.

Having these associated methods on the RouteAgentDispatcher type would be a great improvement in my opinion.

@slushy-chivalry
Copy link

What about the way router_link does it? It doesn't feel too verbose to me (perhaps just me) and avoids hard-coding route strings.

fn update(&mut self, msg: Self::Message) -> ShouldRender {
    // ...
    Msg::MessageThatChangesRoute => {
        self.router.send(RouteRequest::ChangeRoute(AppRoute::OtherRoute.into()));
        false
    },
    // ..
}

@kellpossible
Copy link
Contributor

kellpossible commented Aug 11, 2020

Perhaps there are some ideas in this fork that I've been hacking away on that could be brought upstream?

https://github.com/kellpossible/switch-router/blob/master/src/service/mod.rs

@ranile ranile mentioned this issue Mar 21, 2021
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew-router Area: The yew-router crate feature-request A feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants