erikringsmuth / app-router

Router for Web Components
https://erikringsmuth.github.io/app-router/
MIT License
610 stars 83 forks source link

want a more declarative redirection #23

Closed benzen closed 9 years ago

benzen commented 9 years ago

This is a more a proposition than an issue.

Using your tag, I've done a small app to try out polymer.

Doing so I sayed myself that i'd like to have a nice way to trigger a redirection without leaking out all the page in the app.

So i've used custom event to make my pages ask for redirection. Each page will fire an event, which will eventually be catch by the app-router.

When the router get this event, he find the route that name with the name given with the event. And then redirect to the founded route.

Would you like to add this to your element ? If so i'l ll make a PR.

Want to see what i'm talking about ? have a look at https://github.com/benzen/runner

erikringsmuth commented 9 years ago

I see what you're saying. You're firing this event.

https://github.com/benzen/runner/blob/master/src/front-end/pages/tracks/add-track.jade#L57

this.fire("goto", "tracks stats");

Then listening to it and looking for the route with that name.

https://github.com/benzen/runner/blob/master/src/front-end/index.jade#L31

this.addEventListener("goto", function(event) {
  ...
  location.replace("#"+route[0].attributes.path.value);
});

I've avoided named routes so far. The way I see it, the path is the name of the route. Instead of calling fire you can achieve the same thing by calling the DOM API directly in your add-track element.

location.replace("#/tracks/stats");

This fires a hashchange event which the router already listens to.

benzen commented 9 years ago

The reason for my proposition is to make it easier to change the route without having to search the whole project for "location.replace". In my opinion it is quite normal to as to the router the change the view.

But it's an opinion thing.

erikringsmuth commented 9 years ago

What do you mean by "search the whole project"?

benzen commented 9 years ago

search and replace in editor

2014-09-16 18:45 GMT-04:00 Erik Ringsmuth notifications@github.com:

What do you mean by "search the whole project"?

— Reply to this email directly or view it on GitHub https://github.com/erikringsmuth/app-router/issues/23#issuecomment-55825376 .

Benjamin DREUX

erikringsmuth commented 9 years ago

It'd be exactly the same if you search and replaced the route name or the route path. It's an exact string match either way.

Listening to an event like goto has another tricky part if the route contains path variables like /orders/:orderId. You'd have to pass an orderId on the event to make it navigate correctly. Instead, if you navigate with location.replace('#/orders/123'), location.hash = '/orders/123', history.pushState(), or history.replaceState() it does the work for you.

benzen commented 9 years ago

Yeah right, didn't think about that