Ember 2.x Ember 2.x

Transitioning Routes

Check out a free preview of the full Ember 2.x course:
The "Transitioning Routes" Lesson is part of the full, Ember 2.x course featured in this preview video. Here's what you'd learn in this lesson:

Ember provides methods like transitionTo() and replaceWith() to transition from within a route. Mike walks through a few use cases for when to use one method over the other.

Get Unlimited Access Now

Transcript from the "Transitioning Routes" Lesson

>> [MUSIC]

[00:00:03] We're done with the hooks I wanna go over. There are more but these are the most common ones, the ones we looked at. So now we're talking about transitioning from within a wrap. So this would be for example if, you go to a particular URL like if you go to org/facebook/react, and I want to basically redirect to org/facebook/react/repost.

[00:00:30] I want that to take you to the list of reposts by default. You can use something called transitionTo or replaceWith. And the signatures are very similar, the arguments are the same. And you can use them in a couple different ways, depending on whether you have dynamic segments that you need to populate.

[00:00:53] So you pass in the route name, you pass in the models that are necessary in order to populate that dynamic segment to actually be models or IDs which will just directly be placed into the URL. And then there's this options object if you wanted to use query params, things like that.

[00:01:09] So you can even transition to the current route, but add a query param and this will be a way of accomplishing that. Does this make sense to everybody? Why replace with, not transition tos?
>> Great question, and I forgot to explain that, I'm sorry. transitionTo will add to the history stack, replaceWith will replace the top most frame in the history stack.

[00:01:36] So if you find that you're hitting the back button and something weird is happening, you should consider whether replaceWith or transitionTo is the more appropriate thing to do.
>> They're asking, if controllers are going away, why do we need to do the really important stuff to set them up?

>> There's a high degree of overlap between what controllers do and what components do, which we will talk about later. And once the work is done, which is a soon upcoming feature, for what's called routable components, you will have a component, it'll be setup, I don't know what the actual signature of the method's gonna be, but think of it as setup component instead of setup controller.

[00:02:24] And the thing that you will put in those hooks, the purpose of that setup hook, will likely remain the same. It's just an object that is not a singleton, that doesn't stick around for the lifetime of the app, unless introduce the potential for leaking. It won't be a controller anymore, it will be a component which when you navigate away from that route it's torn down, it's destroyed, and if you come back you have a brand new clean instance to work with.