为什么我的'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
我也尝试在路线模型钩子中使用“includes”,如:
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"
}
]
}
Sideloading非常适合 - 没有额外的API调用。难住了。
答案 0 :(得分:0)
async: true
表示单独查找它们(字面意思是指异步获取数据),这样就可以为您提供单独的请求。
所以你想要async:false
注意您的数据包,我希望将该项目的属性称为product_id
,而不是product
。这可能会解决它。
如果不这样做,我就会开始深入Ember检查员,研究Ember认为它正在装载的数据。
答案 1 :(得分:0)
您的关系称为product
,但是您引用的products
包含了该关系。因为您指定了关系的名称,所以多个重要。 (如果我使用错误的关系多数,我的Ruby on Rails后端会抛出错误 - 您的经验可能因您的后端实现而异。)
看看这是否可以解决您的问题:
model() {
return this.get('store').findAll('cart-item', { include: 'product' });
}