transitionTo/transitionToRoute and options hash

With the redirect hook (or afterModel), it adds an interesting dilemma where if a user hits a route that’s sole purpose is to redirect to another route, a history entry is added to the redirect route. Hence hitting back on the browser, just sends them to the redirect hook and sending them right back to where they attempted to leave. The router supports replacing the current url (as opposed to updating the url). The transition object also has a property on it called urlMethod which helps decide whether to replace or update. That being said, there is no official way to set this property.

In my example below, the referrer is unaware of the first songs id, but is aware of the album id. If that was common knowledge we wouldn’t use a redirect hook, we’d just send it there in the first place and skip the redirect route.

redirect: function(){
var firstSong = this.modelFor(‘album’).get(‘songs.firstObject’);
var trans = this.transitionTo(‘songs.view’, firstSong);
// terrible hack to modify the transition object, and trick it into replacing the url instead or updating it
trans.urlMethod = “replace”;
}

I’m not sure if there is a better way to do this, or if this was the intended way to do it. If so I’ll update the documentation to explain it.

It’d be nice if we could send in an options hash for the transitionTo route, but alas, I realize params 2+ are models for the transition.

1 Like

You can use replaceWith instead of transitionTo:

redirect: function(){ 
  var firstSong = this.modelFor('album').get('songs.firstObject'); 
  this.replaceWith('songs.view', firstSong); 
}

Also, AFAIR redirect is deprecated. You can use beforeModel/afterModel instead.

And PR created to better document the replaceWith and replaceRoute documentation. Thanks I was unaware of them. The only issue I have is if you ever link-to a route with the model (or one that doesn’t require a model), the model hook/after model hook isn’t necessarily called. (I do see how this is a rare use case, and could be solved by using an action, and hooking up a slew of other things, but I’m converting a site to ember and leaving stub entry points that redirect to old portions of the site while I get around to recreating them).

Can you clarify this?

Hmm, I can’t. It looks like it is always called, I could have sworn at some point it wasn’t calling it. Maybe it was on a nightly build pre 1.0 and I got it stuck in my head that’s how it was, or possibly I’m just crazy.