ember-data:建模/将hasMany关系加载到同一模型

时间:2018-01-02 20:11:11

标签: ember.js ember-data

我有一个名为product的模型,它是related的字段。 related字段包含相关产品的ID列表。模型如下。

我遇到的问题是侧载相关产品,因此不需要单独的API请求。我有什么工作,但我看到一个弃用错误(我使用queryRecord因为我需要将查询参数传递给后端)

The adapter returned an array for the primary data of a `queryRecord` response. This is deprecated as `queryRecord` should return a single record.

我的product模型是:

export default Model.extend({
    variantSets: hasMany('variant', { async: true }),
    related: hasMany('product', { async: true }),
    name: attr('string'),
    skuid: attr('string'),
    images: attr(),
    prices: attr()
    ...
});

从后端返回的数据是:

{
    "product": {
        "id": 3311,
        "images": {
            "image": "ASAP-888SAHDASKE1401891447538f2a778bf9a.jpg",
        },
        "name": "Sweatshirt - Hooded Sweatshirt with Pocket",
        "prices": {
            "price": 49.95
        },
        "related": [
            627,
            626
        ],
        "skuid": "ASAP888SAHDASKE",
        "variantSets": [
            "ASAP888SAHDASKE_1",
            "ASAP888SAHDASKE_2"
        ]
    },
    "products": [
        {
            "id": 627,
            "images": {
                "image": "885TABK.jpg",
            },
            "name": "T-Shirt - Shamrock (Black)",
            "prices": {
                "price": 19.95
            },
            "related": [
                633,
                631,
                628,
                624
            ],
            "skuid": "ASAP885TABK",
            "variantSets": [
                "ASAP885TABK_1"
            ]
        },
        {
            "id": 626,
            "images": {
                "image": "885TAKE.jpg",
                "large": "885TAKE.jpg",
                "small": "885TAKE.jpg"
            },
            "name": "T-Shirt - Shamrock (Green)",
            "prices": {
                "price": 19.95
            },
            "related": [
                633,
                630,
                628,
                624
            ],
            "skuid": "ASAP885TAKE",
            "variantSets": [
                "ASAP885TAKE_1"
            ]
        }
    ]
}

我认为这可能是“Reflexive Relationship”,但我找不到太多如何设置它的例子。我在我的product模型中试过这个并没有任何运气:

related: hasMany('product', { inverse: 'product', async: true }),
product: belongsTo('product', { inverse: 'related', async: true }),

任何帮助表示赞赏!

0 个答案:

没有答案