Javascript 如何将一个主干模型与另一个关联?
我有以下答复Javascript 如何将一个主干模型与另一个关联?,javascript,json,backbone.js,model,Javascript,Json,Backbone.js,Model,我有以下答复 { "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
我的目标是要有一个具有主干价格模型的主干产品模型
因此,上述结果数组中的每个条目都将生成一个Product and Price主干模型实例
在主干模型之间建立关联是否理想
我如何做到这一点
更新
在尝试推荐的解决方案后,我收到以下错误消息。
未捕获类型错误:对象函数(){return c.apply(this,arguments)}没有方法“fetch”需要重写。示例代码:
var Products = Backbone.Collection.extend({
model: Product,
url: '/products/index.json',
parse: function(response) {
return _.map(response.results, // map each result to a product
function(result){
var product = result.Product;
product.price = new Price(result.Price); // create a price model
return product; // Backbone will convert the product to model
});
}
});
你需要覆盖。示例代码:
var Products = Backbone.Collection.extend({
model: Product,
url: '/products/index.json',
parse: function(response) {
return _.map(response.results, // map each result to a product
function(result){
var product = result.Product;
product.price = new Price(result.Price); // create a price model
return product; // Backbone will convert the product to model
});
}
});
这是我的最新代码。现在它显示了未捕获的TypeError:对象函数(){return c.apply(this,arguments)}没有方法'fetch',请查看我的注释()。这是我的最新代码。现在它显示了未捕获的TypeError:对象函数(){return c.apply(this,arguments)}没有方法'fetch',请查看我的注释()。