Search code examples
javascriptember.jsember-router

The route i came from, or previous route


When i visit one route and then go to another, is where a way to know which route i came from?
I tried document.referrer - this doesn't work. Googling hadn't brought any answers either..

I set up this fiddle for example and testing..


Solution

  • There is no direct solution using the API. Meaning, this information is not part of the public API. However, you can cache this information yourself.

    App.set('lastRoutes', []);
    
    App.BaseRoute = Ember.Route.extend({
        setupController: function() {
            this._super.apply(this, arguments);
            App.get('lastRoutes').pushObject(this.get('routeName'));
        }
    });
    

    Use "setupController", rather than "enter", because when you transition from /users/1 to /users/2 for instance, the enter/exit methods are not executed, whereas the setupController gets executed every time (and is part of the public API). You should refine it if you have nested routes, because this will add the intermediate routes as well. But for your example, it works well.