javascript - Trying to understand Backbone.js routing betwen views -


i new backbone , trying understand concepts. trying create backbone.js application (with underscore templates) of views protected login page , have logout button on them.

the idea if non-logged in user navigates specific view, should see login page , once logs in redirected view tried go to. code looks this

router code

routes: {     ""                  : "home",     "authview"             : "authview", },  login: function(view) {         if(isuserloggedin()) {             $('#content').html(view.el);         } else {             var loginview = new loginview(view.model)             $('#content').html(loginview.el);             app.vent.on("login:success", function(){                 loginview.remove();                 $('#content').html(view.el);             });         }     },  authview: function() {     this.login(new authview()); } 

authview

authview = backbone.view.extend({    events: {     "click #logoutbutton": "logout"   },    initialize:function () {     this.render();   },    render:function () {     $(this.el).html(this.template(this.model));     return this;    },   logout:function (e) {     e.preventdefault();     loguserout();     app.router.navigate("authview", {trigger:true});   } }); 

the generic login route protection seems work not sure if best way (as view not auth protected, route).

my issue not sure how handle logout. idea user thrown login page , should login again, should routed same view came when pressed logout button.

the current idea recall same route login function called seems backbone not if navigate function trying navigate route it's in. calling view .render() method in case not options since bypasses authentication code (which on route).

how on handle in backbone? on complicating things? should authentication protection placed in view itself? thought of couldn't arrive @ solution did not seem overcomplicated.

i override router's execute function follows

execute: function (callback, args, routename) {     // ..     if ( /* check if user not authenticated */ ) {         backbone.history.navigate("logout", {trigger: true});         return;     }     if ( callback ) {         callback.apply(this, args);     } } //.. // logout route logout: function () {     // logout logic..     // ..     backbone.history.navigate("login", {trigger: true}); } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -