I know that there are a lot of tutorials / information on organizing the application using Backbone.js. I create one gradually using these tutorials. I don't seem to understand the concept of routers. Well, I know that routers are the starting points (and report the status of the application), and it's better to avoid adding more logic there.
I have one router. My app first loaded the header, footer and body of the page. Basically, firstly, I have to log in to the user system. To load the login page, I would do the following:
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, the mail page will be loaded. MailView has some events for displaying incoming messages, for example, the sender.
events: { "click .inbox": "showInbox", "click .sentbox": "showSentbox", }, showInbox : function() {
At this point, I want the router to show ../#inbox and ../#sentbox respectively. I am wondering if I should name one of the router methods here to show this in the address bar. Why am I confusing because he said that using one router is better than more. If I do this, my AppRouter will be more complex. Another problem is that if the user types the address directly, I have to load this page. I think this requires the logic to be built into the AppRouter.
Please tell me the right approach here. Thanks in advance!
source share