2014-12-26 19 views
0

這裏是下面的代碼的描述:如何分離代碼:我的控制器做了太多工作嗎?

  1. 路由器決定哪個控制器方法調用
  2. 控制器獲取模型(一個或多個)
  3. 控制器實例化與模型各種視圖
  4. 控制器實例佈局,放意見納入它
  5. 操控者將佈局到應用程序

是控制器做了太多事情?我猜的不錯的方法應該是

  1. 路由器決定哪個控制器方法調用
  2. 控制器獲取模型(一個或多個)
  3. 控制器實例化佈局與模型
  4. 操控者將佈局到應用程序。 結束控制器的工作
  5. 佈局初始化實例化時與模型

問題觀點:是第二種方法比較好? 如果是這樣,該怎麼辦[3。和5.好方法]?

代碼也jsfiddle

ContactMgr.Router = Marionette.AppRouter.extend({ 
    appRoutes: { 
    'contacts/:id'  : 'detail' 
    } 
}); 

ContactMgr.Controller = Marionette.Controller.extend({ 
    detail: function (id) { 
     var promise = App.request('contact:entities', id); 
     $.when(promise).done(function (contacts) { 
     var _model = contacts.get(id); 

     var contactView = new MyContactView({ model: _model }); 
     var sideView = new MySideView({ model: _model }); 

     var view = new MyLayout(); 
     // MyLayout has mainRegion, sideRegion 
     view.on('show', function (v) { 
      v.getRegion('mainRegion').show(contactView); 
      v.getRegion('sideRegion').show(sideView); 
     }); 

     App.getRegion('contentRegion').show(view); 
     // App has contentRegion, other regions 

     });// when done, end 
    }// detail, end 
}); 
+0

如果這個問題被關閉,那麼你或許應該問的[程序員堆棧交易所(http://programmers.stackexchange.com/)。他們很樂意談論設計問題。 – jww

回答

0

This可能是答案。

而且

ContactMgr.Controller = Marionette.Controller.extend({ 
    detail: function (id) { 
    ... 
    var _model = contacts.get(id); 
    ... 
    var view = new MyLayout({model: _model}); 
    App.getRegion('contentRegion').show(view); 
    } 
}); 

MyLayout = Marionette.Layout.extend({ 
    ... 
    regions: { 
     mainRegion: '#...', 
     sideRegion: '#...' 
    }, 
    contactView: null, 
    sideView: null, 
    onShow: function() { 
     this.getRegion('mainRegion').show(this.contactView); 
     this.getRegion('sideRegion').show(this.sideView); 
    }, 
    initialize: function (opt) { 
     var _model = opt.model; 
     this.contactView = new Marionette.ItemView({ model: _model }); 
     this.sideView = new Marionette.ItemView({ model: _model }); 
    } 

}); 
相關問題