Jacob van Lingen
Jacob van Lingen

Reputation: 9537

How to properly filter ArrayController

I want to filter an existing ArrayController by a subproperty. How to do that? My filter does not work or is empty, as I don't see any items appear (and if i do not filter, i see them).

Here is a simplified version of my code:

Models

App.Post = DS.Model.extend({
    user: DS.belongsTo('user')
    title: DS.attr('string'),
    ...
});
App.User = DS.Model.extend({
    name: DS.attr('string'),
    status: DS.belongsTo('status'),
    ...
});
App.Status = DS.Model.extend({
    title: DS.attr('string'),
    techName: DS.attr('string')
});

Controllers

App.PostsController = Ember.ArrayController.extend({
    activeUsers: function() { 
        return this.filterBy('user.status.techName', 'active'); 
    }.property('@each','@each.user','@each.user.status','@each.user.status.techName')
 });
App.ActiveUsersController = Ember.ArrayController.extend({
    needs: ['posts']
});

Template (my active-users template)

<ul>
    {{#each controllers.posts.activeUsers}}
        <li>{{name}}</li>
    {{/each}}
</ul>

Upvotes: 3

Views: 457

Answers (1)

Hyder
Hyder

Reputation: 1463

Ember.computed.filterBy will satisfy your requirement.

App.PostsController = Ember.ArrayController.extend({ 
    activeUsers: Ember.computed.filterBy('content','user.status.techName','active'),  
 }); 

This is not documented yet, but you can learn from the comments in the source code here

A sample JSBin

Upvotes: 3

Related Questions