Nikhil Agrawal
Nikhil Agrawal

Reputation: 26538

Getting raw reponse after fetching backbone collection

I have an json response from the server like this for backbone collection

 {
       "Heading":[
          {
             "heading1":"heading1",
             "heading2":"heading2",
             "heading3":"heading3",
             "heading4":"heading4",
             "heading5":"heading5",
             "heading6":"heading6",
             "heading7":"heading7"
          }
       ],
       "Data":[
          {
             "column1":98310,
             "column2":"spl",
             "column3":"sreedhar Laptop",
             "column4":"LAPTOP",
             "column5":"ACTIVE",
             "column6":true,
             "column7":"c56e4b92-debe-4c8e-9472-bbb6a322346d"
          },
          {
             "column1":196609,
             "column2":"NLP",
             "column3":"NLP testing..",
             "column4":"LAPTOP",
             "column5":"ACTIVE",
             "column6":true,
             "column7":"7fe2efd4-b93b-4ea8-98a4-7a75d77efb77"
          },
          {
             "column1":262146,
             "column2":"venky",
             "column3":"venkyLaptop",
             "column4":"DESKTOP",
             "column5":"INACTIVE",
             "column6":false,
             "column7":"2e512b95-e2b3-414c-8b40-3bd00b626ae4"
          }
       ]
    }

So What I want is to pass only the Data aray for the collection so I have used parse method like this

Customer = Backbone.Collection.extend({
    model: customerModel,

    url: function () {

        return Config.serviceURL("getCusData");


    },
    parse: function (response) {

        return response.Data;
    }


});

This is working perfectly but Now I want that Heading array also after I apply fetch So how can I get this Heading array on calling fetch??

Upvotes: 1

Views: 324

Answers (1)

mu is too short
mu is too short

Reputation: 434785

There's no rule that says that parse can't stash parts of response in this. For example:

parse: function(response) {
    this.headings = response.Heading;
    return response.Data;
}

Then later the collection can look at this.headings to see the response.Heading values.

Demo: http://jsfiddle.net/ambiguous/k9aUa/

If you're using other methods (such as reset) to populate your collection then you might need to override those to reset this.headings.

Upvotes: 1

Related Questions