backbone.js - call route inside of a view in Backbone -


i know there great deal of tutorials/information orgaising application using backbone.js out there. creating gradually 1 aid of tutorials. seem fail understanding concept of routers. well, aware routers starting point(and tells state of app) , it's better avoid putting more logic there.

i have 1 router. app firstly load header, footer, , main part of page. in main part, first, should login user. load login page, this:

       var approuter = backbone.router.extend({             initialize : function() {             var headerview = new headerview({el:$('#header')});             headerview.render;              var footerview = new footerview({el:$('#footer')});             footerview.render;              var loginview = new loginview({el:$('#login')});             loginview.render;         },         routes : {             "inbox" : "inbox",             "sentbox : "sentbox"         },         inbox : function() {             // ...         },         sentbox : function() {             // ...         }     });     app = new approuter();     backbone.history.start(); 

after successful login, mail page loaded. mailview has events show inbox, sentbox example.

     events: {         "click .inbox": "showinbox",         "click .sentbox": "showsentbox",       },        showinbox : function() {             // app.route() or app.inbox() - ?       } 

at point, want router show ../#inbox , ../#sentbox respectively. wondering if should call here 1 of router's method show in address bar. why confusing because it's said usage of 1 router better more. if so, approuter more complicated. issue if users type address directly, should load page. think requires put logic inside approuter.

please tell me right approach here. beforehand!

check out router.navigate() (http://documentcloud.github.com/backbone/#router-navigate)

i save instance variable of router inside app object, e.g.

var app = {   views: {},   routers: {},   models: {},   data: {}    initialize: function() {     this.data.mainrouter = new app.routers.mainrouter();   } }; 

where app.mainrouter defined as:

app.routers.mainrouter = backbone.router.extend({     routes: {      'route1' : 'route1handler',      'route2/:param' : 'route2handler'    },     ...  }); 

then within view, if want navigate new route, call:

app.data.mainrouter.navigate('route2/param1'); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -