2012-02-22 77 views
0

我从Backbone开始,我想实现一个简单的应用程序以管理用户和产品对象。页面布局总是相同的:标题(首页),菜单(左列)和内容(右列),但标题和菜单内容取决于当前模块(用户或产品)。布局和视图管理

我搜索正确的方式来管理我的页面布局。实际上,我在我的Backbone.Router的每种方法中管理标题和菜单,但我认为这不是最好的解决方案。

var appRouter = Backbone.Router.extend({ 
    routes: { 
     "users": "listUser", 
     "users/new": "newUser", 
     "users/:id": "showUser", 
     "products": "listProduct", 
     "products/new": "newProduct", 
     "products/:id": "showProduct" 
    }, 

    listUser: function() { 
     if (this.userHeaderView == null) { 
      var header= new UserHeaderView(); 
      header.render(); 
      this.userHeaderView = header; 
     } 
     if (this.userMenuView == null) { 
      var menu= new UserMenuView(); 
      menu.render(); 
      this.userMenuView = menu; 
     } 
     this.contentView = new UserListView().render(); 
    } 

    // ... 

    newProduct: function() { 
     if (this.productHeaderView == null) { 
      var header= new ProductHeaderView(); 
      header.render(); 
      this.productHeaderView = header; 
     } 
     if (this.productMenuView == null) { 
      var menu= new ProductMenuView(); 
      menu.render(); 
      this.productMenuView = menu; 
     } 
     this.contentView = new NewProductView().render(); 
    } 

    // ... 
}); 

回答

4

路由器不应该用来管理应用程序初始化。你应该创建一个应用程序对象来管理初始化过程,设置你始终需要的标题和菜单等。(请注意,Backbone不包含这样的对象,这取决于你创建这个对象)。

路由器应该只知道它绝对需要知道什么,以便让应用程序回到被请求的状态。

我已经写在这几篇文章,虽然我的文章可做参考我Backbone.Marionette框架,我在说什么适用于标准的骨干使用,以及原则:

http://lostechies.com/derickbailey/2012/02/06/3-stages-of-a-backbone-applications-startup/

http://lostechies.com/derickbailey/2012/01/02/reducing-backbone-routers-to-nothing-more-than-configuration/

http://lostechies.com/derickbailey/2011/12/27/the-responsibilities-of-the-various-pieces-of-backbone-js/

http://lostechies.com/derickbailey/2011/12/12/composite-js-apps-regions-and-region-managers/

http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

http://lostechies.com/derickbailey/2011/08/30/dont-limit-your-backbone-apps-to-backbone-constructs/

我意识到,我建议你读比大概需要直接回答您的问题更多的信息。但是,这些文章的结合会告诉你许多不同的想法和观点,这些想法和观点将有助于塑造你对Backbone的使用,以便避免在路由器,管理布局,关闭视图等方面的一些常见错误。

+0

感谢您的回复,我已阅读所有文章,并且您的RegionManager似乎很有趣。但是,我不明白我只能在模块更改时才更改标题。 – sylmandel 2012-02-23 08:38:35