Question

given a modal to log in the user I want to return to the path from where the user pressed the log in button. As some other questions suggest I tried to save the previous path in the application controller to a global created in App under the name prevPath:

            pathMemory = null,
            previousPath: function(){
                if (this.pathMemory){
                    App.set('prevPath',this.pathMemory);
                }
                this.pathMemory = this.get('currentPath');
            }.observes('currentPath')

The problem is that I have dynamic paths so the router does not know what to load when I give him the prevPath.

The current solution is to use window.history.back, which is ugly and I suppose not the best way to go.

Était-ce utile?

La solution

In the client side authentication of embercasts, have a nice way to handle it:

Basically, you have a App.AuthenticatedRoute, where all routes that need authentication will extend. In the beforeModel hook, is checked if the user is authenticated, in that case the presence of the token. If the token isn't present the current transition is stored.

App.AuthenticatedRoute = Ember.Route.extend({
  ...
  beforeModel: function(transition) {
    if (!this.controllerFor('login').get('token')) {
      this.redirectToLogin(transition);
    }
  },

  redirectToLogin: function(transition) {
    alert('You must log in!');

    var loginController = this.controllerFor('login');
    loginController.set('attemptedTransition', transition);
    this.transitionTo('login');
  }
  ...
});

When the login is performed and valid, the previous transition is take by self.get('attemptedTransition'), and is called retry. This will retry the transition, in the case, the transition where the user attempted go to, before the login authentication redirect:

...
var attemptedTransition = self.get('attemptedTransition');
  if (attemptedTransition) {
    attemptedTransition.retry();
    self.set('attemptedTransition', null);
  } else {
    // Redirect to 'articles' by default.
    self.transitionToRoute('articles');
  }
  ...

With this, you will have the same behavior.

I hope it helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top