mardi 2 janvier 2018

ember-data: modeling/sideloading a hasMany relationship to the same model

I have a model called product that as a field of related. The related field contains a list of ids of related products. Model below.

The issue I'm having is with side-loading the related products so that separate API requests are not needed. What I have works, but I am seeing a deprecation error (I am using queryRecord because I need to pass query params to back end)

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

My product model is:

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

The data being returned from the back-end is:

{
    "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"
            ]
        }
    ]
}

I am thinking this might be a "Reflexive Relationship" but I can't find too many examples of how to set that up. I tried this in my product model and didn't have any luck:

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

Any help appreciated!




Aucun commentaire:

Enregistrer un commentaire