Sysrq147
Sysrq147

Reputation: 1367

BackboneJS .fetch() put one object from response in collection

My server response is having two objects (picture): enter image description here

How can i put just secound object members (models) in my Backbone Collection. I am using collection.fetch to get data from server. Can I somehowe addapt my server response.

Upvotes: 3

Views: 1714

Answers (2)

Yurui Zhang
Yurui Zhang

Reputation: 2232

use parse()

see: http://backbonejs.org/#Collection-parse

in your collection:

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"];
  }

});

Upvotes: 2

homtg
homtg

Reputation: 1999

You can do this by overriding the parse method of your collection:

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

Your Collection will contain what you return from your parse function, in this case you reduce it to the statuses array from your server response.

Upvotes: 7

Related Questions