Filter the store result

Hi,

. what is the best way to filter my store result? If I try any way to use the filter method, my result will be NULL. Unfortunately I didn’t found no other way to filter the categories list object but by the way … My rendered template is still empty.

This is my current code [EDITED]:

categories: computed('store', function() {
      return this.get('store').findAll('category').then((categories) => {

        return new Promise((resolve) => {
          let filteredCategories = [];

          categories.filter((category) => {
            if (category.get('title').indexOf('*') === -1) {
              filteredCategories.push(category);
            }
          });
          resolve(filteredCategories);
        });
      });
    }),

Best regards, Mario

hey, I think you need to refactor that code a little bit. First thing is findAll returns a promise object not an actual return object, so it’s better to do request in sepearte computed property, or even better in model hook.

Then you can easily write computed property that waits for model and then resolves with filtered data

check here, I’ve prepared small twiddle for you

Remember that in templates you don’t have access to routes properties, only to those in controller, so define properties there or use setupController hook

good luck in learning Ember

Hi CezaryH,

Thanks for your help and code snippet. We solved our problem:

categories: computed('store', function() {
    return DS.PromiseArray.create({
        promise: new Promise((resolve)=> {
            this.get('store').findAll('category').then((categories)=>{
                let filteredCategories = categories.filter((category)=>{
                    return category.get('title').indexOf('*') === -1;
                });
                resolve(filteredCategories);
            })
        })
    });
}),

The problem was only a wrong class type of our filtered list. Ember expected a “PromiseArray” in the template.

These soluten works fine.

Best regards, Mario