Filter the store result


#1

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


#2

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 https://ember-twiddle.com/35ee0785a7e9313d8373f3c0a795ded8

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


#3

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