Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用主干集合获取此JSON结果?_Javascript_Json_Backbone.js - Fatal编程技术网

Javascript 如何使用主干集合获取此JSON结果?

Javascript 如何使用主干集合获取此JSON结果?,javascript,json,backbone.js,Javascript,Json,Backbone.js,我有以下答复 { "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

我有以下答复

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

(函数($){
window.AppView=Backbone.View.extend({
el:$(“正文”)
});
var-appview=新的appview;
var Products=Backbone.Collection.extend({
型号:产品,
url:“/products/index.json”
});
Products.fetch();
})(jQuery);
我想我把
产品
系列写错了

请告知

更新

这是我的新代码


这是我现在收到的错误消息:uncaughttypeerror:Object函数(){return c.apply(This,arguments)}没有方法“fetch”

您的
产品
集合
类,而不是
集合
对象。将代码更改为

new Products().fetch()


应该有用。此外,如果您想自定义如何解析JSON回复,可以重写。

谢谢。根据你的回答,我有一个后续问题。基本上,它需要将JSON应答解析为2个模型,而不是1个。请告知。非常感谢。我从概念上理解了你的答案。但这对我不起作用,因此我不接受这个答案。这是我的新代码,我的新错误消息是uncaughttypeerror:Object函数(){returnc.apply(This,arguments)}没有“fetch”方法,请注意,您接受了我的第二个建议。很抱歉,我忘记了在主干.Collection.extend(…)周围加括号(答案中已修复)。现在它应该可以工作了。我已经接受了您的代码,并添加了一些更改,使其更易于查看。非常感谢。我很高兴能帮助您。首先,您的JSON产品代表是不幸的。在这种情况下,价格应该是产品的后代,而不是兄弟姐妹。然后您应该使用.parse方法告诉集合从结果数组中获取项。
var Products = new (Backbone.Collection.extend(...))