lundi 3 avril 2017

Ember: accessing sideloaded data in setupController with ember-data

I'm using RESTAdapter and trying to figure out how to access sideloaded data.
A sample of the payload is:

{
    "category": {
        "categoryName": "test category",
        "id": 6,
        "products": [
            4419,
            502,
            3992
        ]
    },
    "products": [{
        "description": "Whatevs",
        "id": 4419,
        "name": "Product 1",
        "price": 114.95,
        "skuid": "S21046"
    }, {
        "description": "Whatevs",
        "id": 502,
        "name": "Product 2",
        "price": 114.95,
        "skuid": "SOLS2594"
    }, {
        "description": "Whatevs",
        "id": 3992,
        "name": "Product 3",
        "price": 114.95,
        "skuid": "S21015"
    }]
}

I can see 'category' and 'product' data models (and data) in the ember inspector, so I know they are being loaded.

I can even access the products in the template model.products. BUT I can't access model.products in the route's setupController. The error I get is:

TypeError: Cannot read property '_relationships' of undefined

This is really perplexing me! My route model hook is:

model(params) {
    return this.get('store').queryRecord('category', {
        id: params.id
    })
}

The setupController hook (that causes the error) is:

setupController(controller, model) {
    controller.set('results', model.products);
}

The 'category' model:

export default DS.Model.extend({
    products: hasMany('product'),
    categoryName: attr('string')
});

The 'product' model:

export default DS.Model.extend({

    name: attr('string'),
    skuid: attr('string'),
    price: attr('number'),
    description: attr('string')

});

My template (which works, if I remove the 'setupController' hook from the route):


 <br />


I'd like to be able to access model.products from the route's setupController so I can call it something else. Any help appreciated.




Aucun commentaire:

Enregistrer un commentaire