2013-02-12 61 views
1

我有一個編輯視圖,用於在每次單擊該元素時創建的主幹模型。我遇到的問題是編輯視圖需要兩個Backbone集合來創建編輯表單(它包含兩個<select>列表)。Backbone.js - 使用子模型構建視圖

的觀點:

MyApp.elementView = Backbone.View.extend({ 
    events: { 
      'click .edit': 'editForm', 
    }, 
    editForm: function(ev) { 

      var editView = new TimeTrack.Views.EditJob({ 
       model: this.model 
       // This view needs two more collections 
       // for the <select> elements 
      }); 
      ... 
    } 
}); 

實例化視圖:

var elementView = new MyApp.elementView({ 
     collection: elementCollection 
}); 

什麼是推動所需的集合來編輯視圖的最佳方式?我是否需要從elementView的窗體實例化中傳遞編輯視圖的集合?或者有更好的方法來做到這一點?

回答

3

我這樣做了,通過查看2集合,1 - 主要和其他如下: to elementView - 第二集合,並在elementView recive她。

例如:路由器 我

initialize: -> 
    (YourNameSpace).secondCollection = new (YourNameSpace).secondCollection 

elements: => 
    view = new (YourNameSpace).elementView(secondCollection: @secondCollection) 
    $('.l-yield').html(view.render().el)