0
取藏品我有一個JSON文件基本上是這樣的:BackboneJS - 從模型
[
{
"First" : [...]
},
{
"Second" : [...]
},
{
"Third" : [...]
},
]
在我的路由器,我有:
this.totalCollection = new TotalCollection();
this.totalView = new TotalView({el:'#subContent', collection:this.totalCollection});
this.totalCollection.fetch({success: function(collection) {
self.totalView.collection=collection;
self.totalView.render();
}});
現在我有我的骨幹型號:
define([
"jquery",
"backbone"
],
function($, Backbone) {
var TotalModel = Backbone.Model.extend({
url: "/TotalCollection.json",
initialize: function(opts){
this.first = new First();
this.second = new Second();
this.third = new Third();
this.on("change", this.fetchCollections, this);
},
fetchCollections: function(){
this.first.reset(this.get("First"));
this.second.reset(this.get("Second"));
this.third.reset(this.get("Third"));
}
});
return TotalModel;
});
和我在我的骨幹查看我嘗試呈現該集合:
render: function() {
$(this.el).html(this.template(this.collection.toJSON()));
return this;
}
,但我得到「首先是沒有定義」的錯誤 - 什麼這裏的問題?
啊好吧,這就是正確的:-)所以應該說是在模型嗎?並且應該在花括號內有一些內容{}? – SHT
沒有真的應該,除非你正在做一些特別的每個花括號內必要的 - 否則,你可以很容易地說'this.first =新Backbone.Collection()',它會很好的工作。 – Stephen
好,太棒了!現在我沒有得到任何錯誤。到目前爲止這麼好,但仍然沒有數據:我將我的fetch()添加到了我的問題中。也許有什麼問題呢? – SHT