2013-12-17 54 views
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; 
} 

,但我得到「首先是沒有定義」的錯誤 - 什麼這裏的問題?

回答

1

你有沒有真正定義的變量「第一」,「第二」和「第三」?根據你在這裏展示的內容,這個名字沒有什麼。人們會期望你有一對夫婦行喜歡..

var First = Backbone.Collection.extend({}); 
var Second = Backbone.Collection.extend({}); 
var Third = Backbone.Collection.extend({}); 

但是你沒有提供這樣的事情,所以我的第一個假設是,你只是還沒有定義它。

每評論,這可能是更需要什麼:

render: function() { 
    $(this.el).html(this.template({collection: this.collection.toJSON())}); 
    return this; 
} 

然後..

{{#each collection}} 
    {{#each First}} 
     /*---*/ 
    {{/each}} 
{{/each}} 
+0

啊好吧,這就是正確的:-)所以應該說是在模型嗎?並且應該在花括號內有一些內容{}? – SHT

+0

沒有真的應該,除非你正在做一些特別的每個花括號內必要的 - 否則,你可以很容易地說'this.first =新Backbone.Collection()',它會很好的工作。 – Stephen

+0

好,太棒了!現在我沒有得到任何錯誤。到目前爲止這麼好,但仍然沒有數據:我將我的fetch()添加到了我的問題中。也許有什麼問題呢? – SHT