hussfelt
hussfelt

Reputation: 251

How to access the route inside an ember initializer

We're trying to create a check for a session inside the initializer and if there is none just route to our Sesssion route.

I can't seem to get a hold of and trigger transitionToRoute in any way, how do you do that?

export function initialize(container, application) {
    // Check if we now have an authToken, else transition to session
    if (container.lookup('service:session').get('authToken') === null) {
        var applicationRoute = container.lookup('route:index');
        // Below does not work
        //applicationRoute.transitionTo('session');
    }
}

export default {
  name: 'session-check',
  after: 'session-service',
  initialize: initialize
};

Note: The "session-service" has the after option store.

Upvotes: 2

Views: 640

Answers (1)

givanse
givanse

Reputation: 14943

If you want to perform a transitionTo based on a returned value or variable, the place where you are supposed to do that are the route's hooks beforeModel and afterModel.

Upvotes: 1

Related Questions