2013-10-21 46 views
2

我可以創建一個簡單的模型,像這樣:Chaplin.js如何處理將集合傳遞給視圖?

define(["models/base/model"], function(Model) { 
    "use strict"; 

    var IssueModel = Model.extend({ 
    defaults:{ 
     lastName: "Bob", 
     firstName: "Doe" 
    } 
    }); 
    return IssueModel; 
}); 

然後從我的控制,我可以做到這一點:

this.model = new IssueModel(); 

,然後當我創建我的看法,我可以通過它我的模型,像這樣:

this.view = new IssueView({model: this.model}); 

最後,在我的模板,我可以成功地這樣做,得到的模型屬性:

Hi {{firstName}} {{lastName}} 

但是,當我使用定義一個IssueModel收集和我試圖收集傳遞給我的觀點(而不是像模型我以前表現)我無法弄清楚如何引用模型在我把手模板:

var self = this; 
this.collection = new IssueCollection(); 
this.collection.fetch({ 
    success: function(collection) { 
    self.view = new IssueView({collection: collection}); 
    console.log(self.collection); 
    }, 
    error: function(collection, error) { 
    // The collection could not be retrieved. 
    } 
}); 

我知道fetch正確檢索從我Parse.com後端5款車型,因爲這是我得到的控制檯上:

Console output for collection

我問題是這樣的。我知道Chaplin.js使用getTemplateData,但是當我傳遞一個模型時,我不必做任何特別的事情來引用我的視圖中的屬性。我將如何參考,特別是迭代到我在Handlebars模板中傳遞給我的視圖的集合中?

{{#each [Model in the collection I passed to the view]}} 
    {{title}} 
{{/each}} 

回答

1

卓別林將呈現使用CollectionView的集合,它是basicly偵聽您的收藏中的變化和增加/普通視圖的一種推廣相應刪除子視圖。

this.view = new IssueCollectionView({collection: this.collection}); 

也沒有必要使用集合視圖時,因爲它將會被自動使每一個兒童項目添加數據時去等待成功的呼叫。

+0

太好了。感謝有關CollectView自動更新的信息。我沒有意識到這一點。我問了這個問題,後來才知道答案是在卓別林文檔站點上,但側邊欄不工作,所以我沒有看到關於CollectionViews的文檔。不管怎麼說,還是要謝謝你! –

相關問題