-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add FilterableMixin #1288
Add FilterableMixin #1288
Conversation
Works in a similar way to the SortableMixin. Mix this into an ArrayProxy, define the filterProperties and the array will be filtered to only include the objects whose filterProperties are truthy. You can get more advanced filtering by defining a custom filterCondition. The filterCondition is used to determine if the object is to be included in the filtered list. Any item properties used in the filterCondition will need to be lised in filterProperties.
@@ -7,6 +7,7 @@ | |||
require('ember-runtime/system/array_proxy'); | |||
require('ember-runtime/controllers/controller'); | |||
require('ember-runtime/mixins/sortable'); | |||
require('ember-runtime/mixins/filterable'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the require, but the ArrayController does't not implement the mixin, it's just a miss ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to require it somewhere so that it got picked up for the tests. ArrayController seemed the logical place as that is where sortable was required.
Any suggestions on another way to get the files picked up by the test runner?
I totally miss such a feature. It's great ! And you're right, it would be even better if we could change |
It would be nice if the content refiltered if you changed the filterCondition but I don't think this is possible without making the function into a property and forcing it to go through The analogous example in SortableMixin is swapping out the |
These verify that the FilterableMixin works with bound content and updates correctly when the content is swapped out.
@crofty what's going on with this? |
@twinturbo not sure. I'm using it and I have been contacted by others who are using it. I assume we are waiting to see if it is something that the core team want to include in core. @wagenet thoughts? |
@twinturbo I normally combine them through an intermediary array, see: http://matchingnotes.com/ember-array-proxy/multiple-mixins Since ArrayProxy mixins tend to override the |
I think there is an other feature linked to thes ones for an array. This is the selection. Behind these three things now, we have to find something consistent in order to be able to link them together, or just using some of them. |
@sly7-7 would that even matter? Assume |
To be honest I don't know, but there seems to have some conflicts using both Filterable/Sortable on the same Array(Proxy). |
@twinturbo That seems like it could be useful if the overlap was great enough. |
I suggest unifying that way you can use both at once and not either or.
|
If you want to use both you have to wrap in another ArrayProxy |
Ya I thought of that too. I think having one mixin with the sami API for sorting and filtering would be more usable than the alternatives. |
@crofty what do you think of closing this in favor of the arrangeable mixin PR? |
@twinturbo I can see that ArrangeableMixin is convenient if you need to do sorting and filtering, however, this can be easily introduced at the application level by wrapping array proxies around each other. At the framework level, it feels to me as if the mixins should be kept separate. I guess I like the idea of having mixins that are focussed on one specific thing and that can be chained, in any order, to produce the desired behaviour. Some of my reasoning behind this is just that sorting and filtering are such clear concepts and one is often needed without the other. To me, sticking the two together and calling it 'Arrangeable' makes it less clear what it does. I'd prefer to stick with the well established terminology of filtering and sorting and have an easy way to combine the two if needed. Would be interested to hear the thoughts of others on this though. |
@crofty While it may be common to use them separately, it seems like it will also be pretty common to use them together. Having to do two proxies to handle this seems like unnecessary work for the user. |
@wagenet if the framework wants to provide something that does both sorting and filtering then I think it's better to do this by combining the Sortable and Filterable mixins rather than getting rid of them for something that does both. Can't the framework provide something like the following? (note, this is just sketched out, I haven't tested it). var ArrangedProxy = Ember.ArrayProxy.extend({
init: function(){
this._super()
this.set('_filtered', Ember.ArrayProxy.createWithMixins(Ember.FilterableMixin));
Ember.bind(this, '_filtered.content', 'content');
this.set('_sorted', Ember.ArrayProxy.createWithMixins(Ember.SortableMixin));
Ember.bind(this, '_sorted.content', '_filtered.arrangedContent');
},
sortPropertiesBinding: '_sorted.sortProperties',
sortAscendingBinding: '_sorted.sortAscending',
filterPropertiesBinding: '_filtered.filterProperties',
arrangedContentBinding: '_sorted.arrangedContent'
}); It seems a shame to lose the clarity of the sorting and filtering mixins by rolling them into one and calling it 'arrangeable'. I'd prefer it if the framework provided it's 'arrangeable' concept by just combing the two underlying mixins. |
@crofty I think that would less performant because you need to loop two Composing works fine for us but I wouldn't want noobs to figure that kinda
|
Some more discussion on this overall problem is here: #1562 (comment) |
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
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.
I not 100% happy with this API for the following reasons:
Implementation
The implementation of FilterableMixin is largely the same as the SortableMixin.