lundi 15 février 2016

Ember data show length of a hasMany relationship in a template without downloading all the objects of that relationship

I have a product model that hasMany prices. I have a page that displays the products name, code and then the number of prices it has i.e:

<tbody>
  {{#each model as |product id|}}
  <tr>
    <td>{{#link-to "product" product}}{{product.name}}{{/link-to}}</td>
    <td>{{product.code}}</td>
    <td>{{product.prices.length}}</td>
  </tr>
  {{/each}}
</tbody>

The issue I have is that by using product.price.length Ember data is making thousands of requests to get the prices by id. I don't need any information about the actual price on this page. How can I use the length property here without Ember data downloading all the prices?

models/product.js:

export default DS.Model.extend({
  code: DS.attr('string'),
  name: DS.attr('string'),
  prices: DS.hasMany('price', {async: true})
});

models/price.js

export default DS.Model.extend({
  product: DS.belongsTo('product', {async: true}),
  value: DS.attr('number'),
  minUnits: DS.attr('number'),
  maxUnits: DS.attr('number')
});




Aucun commentaire:

Enregistrer un commentaire