【问题标题】:ember-data: Included data not sideloadingember-data:包含的数据不侧载
【发布时间】: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


    【解决方案1】:

    async: true 表示分别查找它们(字面意思是异步获取数据),以便为您提供单独的请求。

    所以你想要async:false

    观察您的数据包,我希望购物车项目上的属性称为product_id,而不是product。这可能会解决它。

    我开始深入研究 Ember 检查器以查看 Ember 认为它正在加载的数据。

    【讨论】:

    • 感谢您的回复!是的,我尝试了 async: false 但收到错误消息,指出数据不在有效负载中。我添加了一个更新,其中包含一个非常相似的模型的详细信息,它可以很好地侧载product 数据。关于 ember-inspector,你能告诉我我应该具体看什么吗?我看到我的product 数据正在加载(数据选项卡),但这是因为我的cart-item 关系正在解决,它只需要进行单独的 API 调用(而不是使用侧载数据)
    • 在您的更新中,您仍然有 async: true - 因此,如果您逐字使用该代码,它仍然不会旁加载数据。正如我上面所说,在 json 中使用“product_id”,而不是产品,看看是否有效。
    • 谢谢!该更新包含来自单独(但类似)模型的代码,其中侧加载确实有效。无论async 是否设置为truefalse,它都有效,或者不存在。我可以这么说,因为没有单独的 API 调用来解决 product 关系。关于 OP (cart-items),我尝试了 product_idproductId,但结果仍然相同(单独的 API 调用()
    【解决方案2】:

    您的关系称为product,但在包含中您引用的是products。因为您要指定关系的名称,所以复数很重要。 (如果我使用错误的关系复数,我的 Ruby on Rails 后端会抛出错误 - 您的体验可能会因您的后端实现而异。)

    看看这是否能解决您的问题:

    model() {
        return this.get('store').findAll('cart-item', { include: 'product' });
    }
    

    【讨论】:

    • 感谢您的回答。试过include: 'product',但没有骰子。仍然获得单独的 API 调用。还尝试将其保留为复数并将关系名称更改为products,但这显然不起作用。我确实发现在 ember 中执行单数/复数有时有点令人困惑,但我确实理解为什么这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多