如何使用这些响应在我的骨干视图中填充
{
"_count":6,
"data":[{
"id":"6",
"continent":"NORTH AMERICA",
"code":"NA",
"sort_order":"6",
"published":"1",
"date_created":"2014-02-02 13:16:54",
"createdbypk":"1",
"date_modified":"2014-02-02 21:17:10",
"modifiedbypk":"1"
},
{
"id":"5",
"continent":"SOUTH AMERICA",
"code":"SA",
"sort_order":"5",
"published":"1",
"date_created":"2014-02-02 13:16:53",
"createdbypk":"1",
"date_modified":"2014-02-02 21:17:00",
"modifiedbypk":"1"
}]
}
我想要的只是“数据”。使用此骨干代码
using this "this.model.get('_count')" will return 6
but this one "this.model.get('continent')" will return nothing.
请帮助
答案 0 :(得分:2)
您需要在集合中定义parse
方法:
parse: function(response){
return response.data;
}
请参阅http://backbonejs.org/#Collection-parse
如链接中所述,您可以在集合定义中添加它,例如
var Continents = Backbone.Collection.extend({
url: BASE_URL + 'api/continents',
model: Continent,
parse: function(response){
return response.data;
}
});
答案 1 :(得分:0)
this.model.get('data')
会返回一个大陆对象数组。
所以this.model.get('data')[0].continent
应该返回“北美”
并且this.model.get('data')[1].continent
应该返回“南美洲”