如何返回到以前的动态路径emberjs [英] How to return to previous dynamic path emberjs

查看:50
本文介绍了如何返回到以前的动态路径emberjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出了一种登录用户的方式,我想返回到用户按下登录"按钮的路径.正如其他一些问题所建议的那样,我尝试将应用程序控制器中的先前路径保存为在 App 中创建的全局文件,其名称为 prevPath :

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')

问题是我有动态路径,所以当我给他 prevPath 时,路由器不知道要加载什么.

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

当前的解决方案是使用 window.history.back ,这很丑陋,我认为这不是最好的方法.

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

推荐答案

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

基本上,您有一个 App.AuthenticatedRoute ,所有需要身份验证的路由都将在其中扩展.在 beforeModel 挂钩中,检查用户是否已通过身份验证,在这种情况下,令牌是否存在.如果令牌不存在,则存储当前过渡.

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');
  }
  ...
});

执行登录并有效后,上一个转换由 self.get('attemptedTransition')进行,并称为 retry .在登录身份验证重定向之前,这将重试过渡(如果是这种情况),即用户尝试进行的过渡:

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.

希望对您有帮助.

这篇关于如何返回到以前的动态路径emberjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆