samedi 15 avril 2017

ember-data async nested relationships

New to ember-data and I'm trying to figure out how to define (and load) products that have multiple sets of variants (like garment & size), and then relate to and load the variant lists themselves (t-shirt, sweatshirt and S,M,L) asyncronously (because sometimes the lists can be quite large).

Something like this is the final goal (two menus):

enter image description here

I am probably approaching this wrong, but I feel like I am close. I'm able to load and access the product and variant sets no problem, I just can't figure out how to fetch the variants themselves. The 'variant' model always shows '0' in ember inspector.

I have 3 models defined:

product (works):

const { attr, hasMany } = DS;
export default DS.Model.extend({
    variantsets: hasMany('variantset'),

    name: attr('string'),
    skuid: attr('string'),
    price: attr('number')
});

variantset (works):

const { attr, hasMany } = DS;
export default DS.Model.extend({
    variants: hasMany('variant', { async: true }),
    type: attr('string')
});

variant (doesn't work):

const { attr, belongsTo } = DS;
export default DS.Model.extend({
    variantset: belongsTo('variantset', { async: true }),

    code: attr('string'),
    description: attr('string')
});

I'm including the variantsets in the payload as such:

{
  "product": {
    "id": 4226, 
    "name": "Widget Shirt", 
    "price": 109.95, 
    "skuid": "WIDG01", 
    "variantsets": [
      {
        "id": "1", 
        "type": "Garment"
      }, 
      {
        "id": "2", 
        "type": "Size"
      }
    ]
  }
}

And specifying this in the serializer:

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
      variantsets: { embedded: 'always' }
    }
});

And that's all great. But as I mentioned, zilch for the variant list even though hitting my endpoint /variant/1 does work when hit directly. Not seeing any errors in the console or anything, it just plain doesn't work. My expectation is that ember would recognize the relationship and go out and fetch each variant list based on the variantset -> variant relationship.

I think I am missing or not understanding something very important! Any help appreciated!




Aucun commentaire:

Enregistrer un commentaire