I have encountered a problem with filtering in templates when I’m trying to show items only once instead of repeating the same item, though coming as a different property. It is easier to show in code:
There probably is some way to do this in the template but I’d say it makes way more sense to do it in the controller from an Ember standpoint. That’s sort of the purpose of the controller. As a general rule of thumb I think the idea is to use as little logic in your templates as possible.
If I was dealing with that sort of data what I’d probably do is make a computed property that returned an object where the keys are categories and the values are arrays of title/body/comment objects. Something like:
Ember.computed('cardsarray', function(){
// I'm sure there's a much more elegant way to do this but...
let cardCats = {};
this.get('cardsarray').forEach((card) => {
let cat = get(card, 'category');
if(!cardCats[cat]) {
cardCats[cat] = [];
}
cardCats[cat].push(card);
});
return cardCats;
}),
// results in something like:
{
cat1: [
{title: "title", body: "body", comment: "comment},
...
],
cat2: [...],
etc...
}
Thank you, I was thinking that it is probably the only way to deal with the object in the controller, however, I thought maybe I missed some helper like {{showOnce}} or something similar, anyway it is true that the logic is better suited in the controller.