Backbone.js 渲染模型数组
嗨,我正在服务器上搜索一个特定的模式,我得到了一个模型列表Backbone.js 渲染模型数组,backbone.js,rendering,fetch,Backbone.js,Rendering,Fetch,嗨,我正在服务器上搜索一个特定的模式,我得到了一个模型列表 {"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() //Book
{"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'}});
我得到了上面提到的模型数组
如何在下划线模板中呈现模型数组?这真的是一种非常糟糕的做法吗?您的收集响应不正确,这就是主干无法正确解析它的原因 如果要
fetch
ing一个集合,则其格式应为:
[{..},{..},{..}…]
对象数组
如果您正在提取
模型,则他们应:
{..}
单对象哈希
因此,如果您修复了响应,那么它将自动加载到集合中,即
[
{"id":"20","name":"The White Tiger ","author":"Arvind Adiga","status":"Read"},
{"id":"23","name":"Tiger and the Apes","author":"Benny Rice", "status":"Read"}
]