gpt4 book ai didi

backbone.js - 一个backbone.js 模型可以包含多个集合吗?

转载 作者:行者123 更新时间:2023-12-01 01:16:43 26 4
gpt4 key购买 nike

我刚刚开始使用backbone.js,并试图围绕建模概念展开思考。我想使用主干 js 创建一个购物车应用程序,与第 3 方 REST api 接口(interface)(不是 rails,不能修改)。

这是 GET 购物车内容的 JSON 响应示例:

        {
"_v" : "12.3",
"currency" : "USD",
"product_sub_total" : 96.00,
"product_total" : 86.00,
"shipping_total" : null,
"tax_total" : null,
"order_total" : null,
"product_items" :
[
{
"product_id" : "123",
"item_text" : "Product foo",
"quantity" : 2.00,
"product_name" : "foo",
"base_price" : 30.00,
"price" : 60.00
},
{
"product_id" : "456",
"item_text" : "Product foo",
"quantity" : 1.00,
"product_name" : "bar",
"base_price" : 40.00,
"price" : 40.00,
"price_adjustments" :
[
{
"promotion_id" : "10% off",
"promotion_link" : "http://example.com/dw/shop/v12_3/promotions/10_percent_off",
"item_text" : "10% off",
"price" : -4.00
}
]
}
],
"order_price_adjustments" :
[
{
"promotion_id" : "10$ off",
"promotion_link" : "http://example.com/dw/shop/v12_3/promotions/10_bugs_off",
"item_text" : "10$ off",
"price" : -10.00
}
]
}

查看这个 JSON 数据,有诸如“product_total”和“shipping_total”之类的汇总数据,其中包含诸如“product_items”和“order_price_adjustments”之类的列表。甚至单个“product_items”也可以有“price_adjustments”的嵌套列表。

如何在backbone.js 中为这个购物车建模?我是否应该为我看到的每个散列(“product_item”、“price_adjustment”)创建一个模型,然后对这些模型的集合进行建模,然后制作一个包含这些集合以及聚合数据的篮子模型?我不知道如何处理这个......

最佳答案

你真的可以随心所欲地做到这一点。如果您不需要对商品或价格调整数据进行任何操作,只需访问它,我就将其保留为 JavaScript 对象。如果您想定义函数来转换和处理该数据,我会定义 ItemPriceAdjustment楷模。

当然,您的 ShoppingCart模型可以具有称为 items 的属性和 priceAdjustments它们是包含这些模型的主干集合。如果您最终没有将它们定义为模型,请将它们保留为普通数组。

我倾向于在创建 Backbone 模型方面犯错,因为它是微不足道的,并且会为您省去进一步决定您应该首先将它们定义为 Backbone 模型的麻烦。

简介 ,我可能会得到一个 ShoppingCart带主干的模型 ItemsPriceAdjustments包含 Item 的集合和 PriceAdjustment楷模。即shoppingCart.get('items')将返回您的 Item 集合s。

关于backbone.js - 一个backbone.js 模型可以包含多个集合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11675879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com