2012-02-22 11 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.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/

私は私はあなたが直接あなたの質問に答えるために、おそらく必要であるよりもはるかに多くの情報を読んで示唆されていますことを実現します。しかし、これらの記事の組み合わせは、ルータに関する一般的な間違いを避けたり、レイアウトを管理したり、ビューを閉じるなど、バックボーンの使用を形作るのに役立つさまざまなアイデアや考え方を知らせるべきです。

+0

あなたの回答をありがとう、私はすべての記事を読んで、あなたのRegionManagerは面白そうだ。しかし、モジュールが変更されたときにのみヘッダをどのように変更できるのか分かりません。 – sylmandel

関連する問題