如何填充骨干集合

时间:2014-02-06 08:59:27

标签: javascript backbone.js

如何使用这些响应在我的骨干视图中填充

{
 "_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.

请帮助

2 个答案:

答案 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应该返回“南美洲”