Javascript 主干路由器:身份验证重定向

Javascript 主干路由器:身份验证重定向,javascript,backbone.js,router,Javascript,Backbone.js,Router,为了触发在执行路由回调之前触发的事件,我使用以下插件: 这很有效。 但是,在检查用户当前是否在后端进行了身份验证之后,我希望将用户重定向到某个路由(触发器设置为true) 我目前正在使用Ajax请求执行此操作,但显然这给我带来了麻烦。 在下面的代码中,ajax回调只有在路由器的“home”回调已经执行(或任何其他路由)之后才会触发。 我怎样才能做到这一点 我有以下代码: var Router = Backbone.Router.extend({ /* --- 1. Route ma

为了触发在执行路由回调之前触发的事件,我使用以下插件:

这很有效。
但是,在检查用户当前是否在后端进行了身份验证之后,我希望将用户重定向到某个路由(触发器设置为true)

我目前正在使用Ajax请求执行此操作,但显然这给我带来了麻烦。
在下面的代码中,ajax回调只有在路由器的“home”回调已经执行(或任何其他路由)之后才会触发。
我怎样才能做到这一点

我有以下代码:

var Router = Backbone.Router.extend({ 

    /* --- 1. Route management --- */ 

    routes: { 
        '': 'landing_page', 
        '(/)login': 'login', 
        '(/)home': 'home', 
        '*actions': 'defaultAction', 
        }, 
    before: function(route, params) { 
        var getAuthStatus = APP.controllers.auth_controller.isLogged(); 
        var self = this; 

        $.when(getAuthStatus).then(function(response){ 
            var auth_status = Object.keys(response.success)[0]; 
            if(auth_status === 'guest'){ 
                console.log(auth_status); 
                self.navigate("login", {trigger: true}); 
            } 
        }); 
    }
}); 
ajax请求:

Auth_controller.prototype.isLogged = function(){ 
    //Check if the user is authenticated 
    var getAuthStatus = this.auth_model.fetch(); 
    return getAuthStatus; 
}; 

编辑:“before”方法中的“return false”将停止执行回调,但在这种情况下,“login”路由的回调也不会被触发。这不是目的

我找到了我所需要的插件:

该插件允许在点击路由之前创建/接收Ajax请求/响应

在路由器中:

    routes: { 
        '': 'landing_page', 
        '(/)login': 'login', 
        '(/)home': 'home', 
        }, 
    /*--- Before filter checks authentication (BB async router plugin) ---*/
    before: { 
        '(/)login': 'redirect_auth', 
        '(/)home': 'redirect_auth', 
        '(/)users/create_role': 'redirect_auth' 
    }, 
    /*--- Checks if the user is logged in and redirects ---*/
    redirect_auth: function(fragment, args, next) { 
        APP.controllers.auth_controller.redirect(fragment, args, next); 
    }
重定向逻辑在auth_控制器对象中实现如下:

/*--- Check if the user is logged ---*/ 
/*--- PHP REST Show (GET) ---*/ 
Auth_controller.prototype.isLogged = function(){ 
    //Check if the user is authenticated 
    var getAuthStatus = this.auth_model.fetch(); 
    return getAuthStatus; 
}; 

/*--- Redirect the user based on auth status (before -> route) ---*/
Auth_controller.prototype.redirect = function(fragment, args, next){ 
    var getAuthStatus = this.isLogged(); 
    var self = this; 

    $.when(getAuthStatus).then(function(response){ 
        var auth_status = Object.keys(response.success)[0]; 
        if(auth_status === 'guest'){ 
            if(fragment === 'login'){
                next(); 
            }else{ 
                APP.router.navigate("login", {trigger: true});                         
            } 
        } else { 
            var sidebars_controller = new Sidebars_controller(); 

            APP.user = response.success["auth"]; 
            if(fragment === 'login'){ 
                APP.router.navigate("home", {trigger: true});                         
            }else{ 
                next(); 
            }
        } 
    })        
};