如何将一个骨干模型与另一个骨干模型相关联?

编程入门 行业动态 更新时间:2024-10-10 14:23: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" } ] } ] }

用于检索上述数据的网址是/products/index.json

The url to retrieve the above data is /products/index.json

要检索产品数据第2页的网址是/products/index.json/page:2

The url to retrieve page 2 of products data is /products/index.json/page:2

我的目标是拥有具有骨干价格模型的骨干产品模型.

My objective is to have a Backbone Product Model that has a Backbone Price Model.

因此,以上结果数组中的每个条目都将生成产品和价格"主干模型实例.

Thus each entry in the above results array, will generate a Product and Price backbone model instance.

在骨干模型之间建立关联是否理想?

Is it ideal to have associations between Backbone models?

我该如何实现?

更新

尝试了推荐的解决方案后,我收到以下错误消息. 未捕获的TypeError:对象函数(){返回c.apply(this,arguments)}没有方法'fetch'

I get the following error message after I tried the recommended solution. Uncaught TypeError: Object function (){return c.apply(this,arguments)} has no method 'fetch'

推荐答案

您需要覆盖 collection.parse .示例代码:

You need override collection.parse. Sample code:

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 }); } });

更多推荐

如何将一个骨干模型与另一个骨干模型相关联?

本文发布于:2023-11-28 14:08:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642722.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:骨干   模型   相关联   如何将

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!