How can I get My previous route?
Asked Answered
C

2

18

How can I get my previous router in my current controller.

App.MyController = Em.ObjectController.extend({
    next:function() { // This is my action helper in HBS
        this.transitionTo('nextPage');
    },
    back:function() { // This is my action helper in HBS
        // Here I need to dynamically identify my previous route.
        // How can I get my previous route.
    }
});
Caveman answered 21/2, 2013 at 10:58 Comment(3)
possible duplicate of #14832168Beitch
I'm also working this issue. It would be nice in a later MVC version if there was something like: Request.RawUrl.PreviousRouteInternational
Just do history.back(). That's how Ember is designed to work. There's no need to build a whole bunch of machinery to maintain your own history. The browser already keeps its own.Fyke
Z
21

After having inspected the router object again, I don't see any property in there that will allow you to grab the last route. In pre 4 there was a property for the last route, but it was a difficult property to work with.

My solution is therefore the same as it was in pre 4: I'd create my own mixin to handle the routes that you navigate into, and from that list of routes, you can get whatever route you're after: the current one, the last one, et cetera...

jsFiddle here: http://jsfiddle.net/sMtNG/

Mixin

The first thing to do is create the mixin that will allow us to push the routes into a HistoryController. We can do this by creating a setupController method which of course gets invoked every time you move into a route.

App.HistoryMixin = Ember.Mixin.create({
    setupController: function() {
        this.controllerFor('history').pushObject(this.get('routeName'));
    }
});

We are pushing the route into the HistoryController.

History Controller

Since we're currently pushing the routeName into a non-existent HistoryController, we'll need to go ahead and create that, which is absolutely nothing special.

App.HistoryController = Ember.ArrayController.extend();

Index Controller

Since the HistoryController stores the list of routes we've navigated into, we'll need it accessible on other controllers, such as the IndexController, we'll therefore use needs to specify in which controller it should be accessible.

App.ApplicationController = Ember.Controller.extend({
    needs: ['history']    
});

Implement Mixin

We now have everything we need to keep a track of the routes, and so we'll specify that our routes need to implement this mixin.

App.CatRoute = Ember.Route.extend(App.HistoryMixin);

Template

Last but not least, now that we have a HistoryController which our IndexController can access, and the mixin pushes each accessed route into the HistoryController, we can use our application view to output a list of the routes, and specify the last route. Of course in your case you'll need the last route minus one, but there's no sense in me doing everything!

<h1>Routes History ({{controllers.history.length}})</h1>
<ul>
    <li>Last Route: {{controllers.history.lastObject}}</li>
    {{#each controllers.history}}
        <li>{{this}}</li>
    {{/each}}
</ul>

I hope this gets you onto the straight and narrow.

Zurkow answered 21/2, 2013 at 17:15 Comment(3)
This works for non dynamic routes. Would this approach work for routes with dynamic segments?Chaise
Don't you need to call _super from your setupController in the mixin?Fyke
Seems way too complicatedHoffert
C
0

Current solutions feel like the tail wagging the dog.

Simple solution is to do window.history.back()

Ember doesn't keep track of the router history, since it would be redundant. All browser already handle this by default.

Conclusion answered 2/4, 2018 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.