获取主干集合后获得原始响应

时间:2013-12-26 06:43:08

标签: javascript jquery backbone.js backbone-collections

我有一个来自服务器的json响应,用于主干集合

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

所以我想要的是只传递集合的数据aray,所以我使用了像这样的解析方法

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

    url: function () {

        return Config.serviceURL("getCusData");


    },
    parse: function (response) {

        return response.Data;
    }


});

这是完美的工作但现在我在应用fetch后也想要那个Heading数组那么如何在调用fetch时得到这个Heading数组?

1 个答案:

答案 0 :(得分:1)

没有规则说parse无法隐藏responsethis的部分内容。例如:

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

之后,该集合可以查看this.headings以查看response.Heading值。

演示:http://jsfiddle.net/ambiguous/k9aUa/

如果您正在使用其他方法(例如reset)来填充您的收藏集,那么您可能需要覆盖这些方法以重置this.headings