2013-10-02 44 views
3

我想通過子屬性過濾現有的ArrayController。怎麼做?我的過濾器不工作或是空的,因爲我沒有看到任何項目出現(如果我不過濾,我看到他們)。如何正確過濾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> 

回答

3

Ember.computed.filterBy將滿足您的要求。

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

這尚未記錄,但你可以從註釋的源代碼學習here

樣本JSBin

+0

THX的男人,正是我需要的! –

+0

很高興幫助:) – Hyder