如何使用 Backbone Collection 获取此 JSON 结果?

2023-12-27

我有以下 JSON 回复

{
   "results":[
      {
         "Product":{
            "id":"1",
            "short_name":"Infra - 2200 CAS Sma SIMO onl [DAS.1.1]",
            "serial_number":"DAS.1.1",
            "created_by":"Wesley Jace Tan",
            "modified_by":"Wesley Jace Tan",
            "created":"2013-02-11 07:58:20",
            "modified":"2013-02-11 07:58:20",
            "full_name_type":"2200",
            "full_name_cas_stk":"CAS",
            "full_name_size":"Small",
            "full_name_simo_mimo":"SIMO only",
            "full_name_product_code":"(2961-737)",
            "uom":"lot",
            "material":"Infra"
         },
         "Price":[
            {
               "id":"1",
               "product_id":"1",
               "source_file":"LTE Test File.xls",
               "for_financial_year":"FY12_13",
               "created_by":"Wesley Jace Tan",
               "modified_by":"Wesley Jace Tan",
               "created":"2013-02-11 07:58:20",
               "modified":"2013-02-11 07:58:20",
               "gross_unit":"50.00",
               "gross_total_value":"0.00",
               "gross_total_formula":"=K12*J12",
               "incentive_value":"5",
               "incentive_formula":"5",
               "net_price_unit":"0.00",
               "net_price_total_value":"0.00",
               "net_price_total_formula":"=N12*J12"
            }
         ]
      },
      {
         "Product":{
            "id":"2",
            "short_name":"Infra - 2200 CAS Sma SIMO to  [DAS.1.2]",
            "serial_number":"DAS.1.2",
            "created_by":"Wesley Jace Tan",
            "modified_by":"Wesley Jace Tan",
            "created":"2013-02-11 07:58:20",
            "modified":"2013-02-11 07:58:20",
            "full_name_type":"2200",
            "full_name_cas_stk":"CAS",
            "full_name_size":"Small",
            "full_name_simo_mimo":"SIMO to MIMO Retrofit",
            "full_name_product_code":"(2961-737)",
            "uom":"lot",
            "material":"Infra"
         },
         "Price":[
            {
               "id":"2",
               "product_id":"2",
               "source_file":"LTE Test File.xls",
               "for_financial_year":"FY12_13",
               "created_by":"Wesley Jace Tan",
               "modified_by":"Wesley Jace Tan",
               "created":"2013-02-11 07:58:20",
               "modified":"2013-02-11 07:58:20",
               "gross_unit":"11.00",
               "gross_total_value":"0.00",
               "gross_total_formula":"=K13*J13",
               "incentive_value":"24",
               "incentive_formula":"24",
               "net_price_unit":"0.00",
               "net_price_total_value":"0.00",
               "net_price_total_formula":"=N13*J13"
            }
         ]
      },
      {
         "Product":{
            "id":"3",
            "short_name":"Infra - 2200 CAS Sma Full MIM [DAS.1.3]",
            "serial_number":"DAS.1.3",
            "created_by":"Wesley Jace Tan",
            "modified_by":"Wesley Jace Tan",
            "created":"2013-02-11 07:58:20",
            "modified":"2013-02-11 07:58:20",
            "full_name_type":"2200",
            "full_name_cas_stk":"CAS",
            "full_name_size":"Small",
            "full_name_simo_mimo":"Full MIMO",
            "full_name_product_code":"(2961-737)",
            "uom":"lot",
            "material":"Infra"
         },
         "Price":[
            {
               "id":"3",
               "product_id":"3",
               "source_file":"LTE Test File.xls",
               "for_financial_year":"FY12_13",
               "created_by":"Wesley Jace Tan",
               "modified_by":"Wesley Jace Tan",
               "created":"2013-02-11 07:58:20",
               "modified":"2013-02-11 07:58:20",
               "gross_unit":"12.00",
               "gross_total_value":"0.00",
               "gross_total_formula":"=K14*J14",
               "incentive_value":"5",
               "incentive_formula":"5",
               "net_price_unit":"0.00",
               "net_price_total_value":"0.00",
               "net_price_total_formula":"=N14*J14"
            }
         ]
      }
   ]
}

检索上述数据的 url 是 /products/index.json

检索产品数据第 2 页的 url 为 /products/index.json/page:2

我写了以下内容作为集合

<script type="text/javascript">
    (function ($) {
        window.AppView = Backbone.View.extend({
            el: $("body")
        });
        var appview = new AppView;

        var Products = Backbone.Collection.extend({
            model: Product, 
            url: '/products/index.json'
        });

        Products.fetch();
    })(jQuery);
</script>

我想我已经写了Products收藏错误。

请指教。

UPDATE

这是我的新代码http://jsfiddle.net/Cw6PT/ http://jsfiddle.net/Cw6PT/

这是我现在收到的错误消息: Uncaught TypeError: Object function (){return c.apply(this,arguments)} has no method 'fetch'


Your Products is a Collection类,不是一个Collection目的。将您的代码更改为

new Products().fetch()

or

var Products = new (Backbone.Collection.extend(...))

应该管用。此外,如果你想自定义如何解析 JSON 回复,你可以覆盖集合.解析 http://backbonejs.org/#Collection-parse.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Backbone Collection 获取此 JSON 结果? 的相关文章

随机推荐