2013-04-01 130 views
0

嗨我正在從服務器上的特定模式搜索,我得到一個模型列表回來。渲染陣列的模型

{"0":{"id":"20","name":"The White Tiger ","author":"Arvind Adiga","status":"Read"}, 
    "1":{"id":"23","name":"Tiger and the Apes","author":"Benny Rice", "status":"Read"} } 

我最初試圖通過

 var books = new Books() //Books() is a collection name. 
     books.fetch({data: {name:'tiger'}}); 

獲取整個集合,但漸漸未定義的錯誤。

所以我試圖獲得一個模型數組。

var books = new Book() //Book is a model name 
    books.fetch({data: {name:'tiger'}}); 

我收到了上面提到的模型數組。

如何渲染下劃線模板中的模型數組?這真的是一個非常糟糕的做法嗎?

回答

0

你的集合響應不正確,這就是爲什麼Backbone沒有正確解析它。

如果你是fetch荷蘭國際集團集合比他們應該採用以下格式:

[ { .. }, { .. }, { .. } ... ]對象的數組

如果你是fetch荷蘭國際集團的模型比他們應該:

{ .. }單個對象散列

所以,如果你固定t他的反應,然後它會自動加載到集合,即

[ 
    {"id":"20","name":"The White Tiger ","author":"Arvind Adiga","status":"Read"}, 
    {"id":"23","name":"Tiger and the Apes","author":"Benny Rice", "status":"Read"} 
] 
+0

謝謝。我意識到這一點,我糾正了它。 – Abhishek