【发布时间】:2018-01-03 17:04:34
【问题描述】:
为什么我的“product”数据没有侧载,对任何人来说都很明显吗?我看到每个购物车项目对 /product 的单独 API 请求。我的cart-item 模型是:
export default Model.extend({
cart: belongsTo('cart'),
product: belongsTo('product', { async: true }),
skuid: attr('string'),
quantity: attr('number'),
personalization: attr(''),
variantCodes: attr(''),
variantData: attr(''),
prices: attr('')
});
一个示例 API 有效负载是(我使用的是RESTAdapter):
{
"cart-items": [
{
"cart": "0048f8cc-ef50-11e7-8337-76502ffc62f3",
"id": "ROVE949352B",
"personalization": [],
"prices": {
"price": 74.95,
"totalPrice": 74.95
},
"product": 4464,
"quantity": 1,
"skuid": "ROVE949352B",
"variantCodes": [],
"variantData": []
},
{
"cart": "0048f8cc-ef50-11e7-8337-76502ffc62f3",
"id": "BLK4226",
"personalization": [],
"prices": {
"origprice": 0,
"price": 60,
"totalPrice": 60
},
"product": 4502,
"quantity": 1,
"skuid": "BLK4226",
"variantCodes": [],
"variantData": []
}
],
"products": [
{
"id": 4464,
"images": {
"image": "tristan-irish-watch.jpg",
},
"name": "Watch - Gold Plated Watch",
"prices": {
"price": 74.95
},
"skuid": "ROVE949352B"
},
{
"id": 4502,
"images": {
"image": "BLK4226.jpg",
},
"name": "Serving Bowl",
"prices": {
"origprice": 0,
"price": 60
}
"skuid": "BLK4226"
}
]
}
这种关系运作良好,只是产生了单独的请求。我试过 async: false 但 ember 抱怨数据不存在:
Assertion Failed: You looked up the 'product' relationship on a 'cart-item' with id ROVE949352B but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async
我也尝试在路由模型钩子中使用“包含”,例如:
model() {
return this.get('store').findAll('cart-item', { include: 'products' });
}
任何帮助表示赞赏!
更新
我正在使用 RESTAdapter(不是 JSON:API),并且我正在使用其他模型成功地侧载数据。我一生都无法弄清楚这个有什么不同。例如,这是一个非常相似的,saveditem:
export default Model.extend({
user: belongsTo('user'),
product: belongsTo('product', { async: true }),
dateAdded: attr('number')
});
API 中的负载(侧载产品)如下所示:
{
"saveditems": [
{
"dateAdded": 1514914371,
"id": "f51bf2b8-efe2-11e7-8337-76502ffc62f3",
"product": 4502,
"user": "me"
},
{
"dateAdded": 1514440889,
"id": "8c3a7f96-eb94-11e7-8337-76502ffc62f3",
"product": 1312,
"user": "me"
}
],
"products": [
{
"id": 4502,
"images": {
"image": "BLK4226.jpg",
},
"name": "Serving Bowl",
"prices": {
"origprice": 0,
"price": 60
},
"skuid": "BLK4226"
},
{
"id": 1312,
"images": {
"image": "GC689.jpg",
},
"name": "Classic Cross",
"prices": {
"price": 29.95
},
"skuid": "JDSGC689GCB"
}
]
}
侧载在这方面非常有效 - 无需额外的 API 调用。被难住了。
【问题讨论】:
标签: ember.js ember-data