Is there an easy way to define router redirects?


#1

I have a few different cases where I’d like to define a redirect. As far as I can tell, the only way I can do that is by defining a subclass of Ember.Route for each redirect with a setup that calls this.replaceWith. It would be really nice if the router offered a simple DSL. Something like

// People who bookmarked "/oldFoos" get the right page
router.redirect("/oldFoos", "/foos");

or

// Going to a user means going to her essays unless you specify a different tab:
router.redirect("/users/:id", function(params) {
  return "/users/" + params.id + "/essays";
});

I have a prototype that works for these cases. It creates a new instance of Ember.Route

Ember.Route.create({
  model: function(params) { return params; }
  setup: function(params) { this.redirectTo(destination(params)); }
});

and generates a unique handler name for that route and maps it.

Would anyone else have any use for this?


#2

One problem with this API is that you can’t take advantage of nesting. An alternative API might be

router.map(function(match, redirect) {
  match('/foos/:id').to('show_foo', function(match, redirect) {
    match('/:subsection').to('show_foo_subsection');
    redirect('/').to(function(params) {
      return [ 'show_foo_subsection', params.id, 'bars' ];
    });
  });
});

#3

I like the possibility to let the redirect be specified in the router, and I started a discussion here a few months ago. The outcome was that such a feature might be well suited for an ember-addon package (as stated by @stefan in https://github.com/emberjs/ember.js/pull/2371#issuecomment-16344108)