BackboneJS .fetch()在响应中放置一个对象

时间:2013-11-24 19:34:48

标签: backbone.js response fetch

我的服务器响应有两个对象(图片): enter image description here

我如何在我的Backbone Collection中放置只有对象成员(模型)。我正在使用collection.fetch从服务器获取数据。我能否以某种方式加入我的服务器响应。

2 个答案:

答案 0 :(得分:7)

您可以通过覆盖集合的解析方法来执行此操作:

var coll = Backbone.Collection.extend({
 parse: function(data){
  return data.statuses;
 }
});

您的收藏将包含您从解析函数返回的内容,在这种情况下,您将其从服务器响应中减少到状态数组。

答案 1 :(得分:2)

使用parse()

请参阅: http://backbonejs.org/#Collection-parse

在你的收藏中:

yourCollection = Backbone.Collection.extend({
  //other collection stuff.

  parse: function(response) {
    //save the search metadata in case you need it later
    this.search_meatadata = response["search_metadata"];
    // return the array of objects.
    return response["statuses"];
  }

});
相关问题