mercredi 25 mai 2016

Obtaining related Ember model data from within a route or component

I have a (product category) model with a many to many relationship eg

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

My category route (product.items) is backed by this model, and it's no problem displaying the related product data:


 


However, I need to obtain the related data within the child route (product.items.item). The model hook for the route looks like this:

model(params) {
  let parentModel = this.modelFor('product.items'),
  let model = this.store.findRecord('product',params.id);

  return Ember.RSVP.hash({
    data:model,
    parentData:parentModel
  });
}

I can then then pass this model to a component.

What I am trying to do is access that related within a component (or within the route). Assuming the model is available in the component as parentModel I can access the name of the parent category as

 let name = parentModel.data.get('name');

I thought I could do the same for the related products ie

 let products = parentModel.data.get('products');

However, this doesn't return anything. Am I making a basic mistake here?

The reason I want to access the other products in the parent category, when viewing an item, is that I want the user to paginate through each item.

Using the JSONAPIAdapter.

Many thanks in advance!




Aucun commentaire:

Enregistrer un commentaire