AlexGalays / abyssa-js

Framework agnostic hierarchical router for single page applications
MIT License
71 stars 9 forks source link

Redirect #11

Closed pauldijou closed 11 years ago

pauldijou commented 11 years ago

Is there a way to support redirection? Let's say I had the following state:

contact: State("/contact/:how?subject")

But my boss wants to change all url of the site to:

newContact: State("/something/contact/:how/more/stuff?subject")

It would be awesome to no display a 404 on the old url but rather redirect to the new one, keeping all params and query string and hash of couse.

It would also be useful for "notFound" state, if you want to redirect to home page rather than display a 404 page.

AlexGalays commented 11 years ago

There is a way, is that what you had in mind?

var router = Router({

  contact: State("/contact/:how?subject", function(params) {
      router.redirect('newContact', params);
  })

  newContact: State("/something/contact/:how/more/stuff?subject", function(params) {
     // do the actual work here
  })

});

You could easily create a utility function called Redirect that creates a State which simply redirects to another: and then use it like that:

var router = Router({
  contact: Redirect("/contact/:how?subject", 'newContact'),
  ...
});
pauldijou commented 11 years ago

Sounds good to me. The helper would be nice, this way you can control it in case you change your logic at some point.