To trigger an event that fires before the route callback is executed, I use the following plugin:
https://github.com/boazsender/backbone.routefilter
It works well.
However, I want to redirect the user to a specific route (with the trigger set to true) after checking if the user is checked on the rear panel.
I am currently doing this with an Ajax request, but obviously this is causing me problems.
In the code below, the ajax callback will only start after the home router callback has already been completed (or any other route, for that matter).
How can i do this?
I have the following code:
var Router = Backbone.Router.extend({
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 request:
Auth_controller.prototype.isLogged = function(){
var getAuthStatus = this.auth_model.fetch();
return getAuthStatus;
};
Edit: "return false" in the "before" method will stop the callback, but in this case the callback for the "login" route will also not start. This is not a goal.
Trace source
share