A few things, first off, why is there no groupBy in Ember.Enumerable ? Is there some other way of doing it with the build in functions that I’m not aware of, or am I supposed to do it somewhere other than my controller?
I’m still very new to ember so I may have missed something. But I have some data I’d like to group and sort by date (and in the groupings, by start time) and I can’t figure out how to do this with ember.
I was looking for one the other day and just ended up doing this. In this case I am grouping by the type property and it’s content is pushed to the contents array. It’s not very optimal, but it works in my use-case. I stuck this on my array controller but it can be applied to anything really.
groupedResults: function () {
var result = [];
this.get('content').forEach(function(item) {
var hasType = result.findBy('type', item.get('type');
if(!hasType) {
result.pushObject(Ember.Object.create({
type: item.get('type'),
contents: []
}));
}
result.findBy('type', item.get('type')).get('contents').pushObject(item);
});
return result;
}.property('content.[]')
Agreed! A groupBy function like this, as well as a sortBy with the same usage would be awesome. It’s definitely something I would use and was surprised to find that this doesn’t exist in ember already