Store filter works or not?

as I am fighting to find a proper way to filter the results of store.find I came cross the filter function on this page. Interestingly enough the filter functionality is not even mentioned on the proper place where somebody would look for it.

however thats not the issue right now, the problem is that the filter simply does not work. when I write

return this.store.find('log');

it works and it returns all the records, but when I write:

return this.store.filter('log', function(log){...});

it always returns empty array. It does not even call the filter function (I checked it by putting some console.log there).

I have also tried these variations:

return this.store.filter('log', {}, function(log){...});

and

return this.store.filter('log', null, function(log){...});

any tip here?

Given the following model:

App.Post = DS.Model.extend({
   category: DS.attr('string'),
   isPublished: DS.attr('boolean')
});

you can query your store for all published records in the Ember category using this.store.find('post', { 'isPublished': true, 'category': 'Ember' })

The documentation is here.

I believe the link in your OP refers to querying the server for records but I’ve never tried that method. Maybe someone else can better address using filter? I’d be interested to learn if/how you can query the store using Inequal operators or LIKE operators.

my filtering is more complex than a simple query and there is a solution to that in ember. please read from here the whole story: https://github.com/emberjs/data/issues/1872#issuecomment-49301748

The relevant API docs are here Store - 4.6 - Ember API Documentation

store.filter will only filter records that are already in the store unless you pass in the second parameter which is a query to be triggered first.

If you want to find and filter all records you could run two queries:

this.store.find('log');
return this.store.filter('log', null, function(log) {...});

Because store.filter returns a live array, it will be populated when the store.find promise resolves.

3 Likes

wow! so simple and it worked perfectly. I think sometimes the situation makes us think about a super complex solution where easier ways work perfectly!