emberjs/ember.js — pull request #1288 by crofty (2012-08-16 15:19:48+00:00)
Title: Add FilterableMixin

Here is my pass at a filterable mixin. I've had a need for something like this in a couple of projects and so I'm putting it here to get some feedback.

The aim is to have an array proxy that can produce a filtered array of items based on a provided filter. The filtering needs to be done intelligently so that as items are added and removed, the entire list is not replaced.

I've put together a fiddle showing the filtered mixin working: http://jsfiddle.net/ydXrU/10/
As items are removed/added to the filtered list, only the newly added item gets rendered.

Compare this to a naive implementation using `Enumerable#filter`: http://jsfiddle.net/bbjaA/1/
The entire list is rerendered each time an item is added/removed.

### API

I'd appreciate thoughts & suggestions on the API.

By default, the items are included in the filtered array if their filterProperties are truthy
```.js
App.filteredCollection = Ember.ArrayProxy.create(Ember.FilterableMixin, {
filterProperties: ['display'], // Any item with a truthy display property will be included
content: content
});
```

More advanced filtering can be done by specifying a filterCondition. However, any of the properties used in the filterCondition will need to be defined in the filterProperties array.

```.js
App.filteredCollection = Ember.ArrayProxy.create(Ember.FilterableMixin, {
filterProperties: ['id', 'name'],
filterCondition: function(item){
return item.get('id') && item.get('name') === 'James';
},
content: content
});
```

I not 100% happy with this API for the following reasons:

- if you are using a custom filterCondition, you need to make sure all the properties used in the filterCondition are listed in filterProperties array. If they are not, then the observers won't all be set up correctly.

- if the filterCondition is swapped out for a different filterCondition, nothing is notified and so the array will not be refiltered.

### Implementation
The implementation of FilterableMixin is largely the same as the SortableMixin.