3

我想通过子属性过滤现有的 ArrayController。怎么做?我的过滤器不起作用或为空,因为我没有看到任何项目出现(如果我不过滤,我会看到它们)。

这是我的代码的简化版本:

楷模

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')
});

控制器

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']
});

模板 (我的活跃用户模板)

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

1 回答 1

3

Ember.computed.filterBy将满足您的要求。

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

这还没有记录,但是您可以从源代码中的注释中了解here

一个示例JSBin

于 2013-10-02T15:36:03.063 回答