0
votes

I have an ember model Category:

export default DS.Model.extend({
  name: DS.attr('string'),
  img: DS.attr('string'),
  url: DS.attr('string'),
  cnt: DS.attr('number'),

//  parent_id: DS.belongsTo('category', {
//    inverse: 'children',
//    async: true
//  }),
  parent_id: DS.attr('string'),

//  children: DS.hasMany('category', {
//    inverse: 'parent_id',
//    async: true
//  }),
  children: DS.attr(),

  isSelected: false,
  isExpanded: false,

  hasChildren: function() {
    return this.get('children').get('length') > 0;
  }.property('children').cacheable(),

  isLeaf: function() {
    return this.get('children').get('length') == 0;
  }.property('children').cacheable()
});

In my index route I have:

export default Ember.Route.extend({
  model: function() {
    var store = this.store;
    return Ember.ArrayProxy.create({
      categories: store.find('category'),
      menuTopCategories: store.find('category', { parent_id: 1 })
    });
  }
});

I'm using a RESTAdapter so the store.find will send two requests to the server: categories and categories?parent_id=1. I would like to have only the first request and then filter through the categories. I tried store.all - since I saw it reuses the already fetch data, but I can't manage to apply the filter.

1

1 Answers

0
votes

I've rewritten the menuTopCategories and I don't see a new request:

menuTopCategories: store.filter('category', function(category) {
   return category.get('parent_id') === "1";
})

My problem right now is to get the root category (first one) without hardcoding the parent_id.