【问题标题】:Getting raw reponse after fetching backbone collection获取骨干集合后获取原始响应
【发布时间】:2013-12-26 06:43:07
【问题描述】:

我有一个来自服务器的 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"
          }
       ]
    }

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

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

    url: function () {

        return Config.serviceURL("getCusData");


    },
    parse: function (response) {

        return response.Data;
    }


});

这工作得很好,但是现在我在应用 fetch 之后也想要那个 Heading 数组那么我怎样才能在调用 fetch 时得到这个 Heading 数组??

【问题讨论】:

    标签: javascript jquery backbone.js backbone.js-collections


    【解决方案1】:

    没有规则说parse 不能将response 的部分内容存储在this 中。例如:

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

    然后集合可以查看this.headings 以查看response.Heading 值。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多