Matt
Matt

Reputation: 1811

EmberJS filter hasMany data, apply to belongsTo relationship

I have this teams model:

export default DS.Model.extend({
    "name": attr('string'),
    "players": DS.hasMany('player',{
        async: true
    })
});

along with this player model:

export default DS.Model.extend({
    "name":  attr('string'),
    "status": attr('string'),
    "team": DS.belongsTo('team',{
        async: true
    })
});

If I want to list out all the teams, easy enough. And if I want to list out all the teams, and within each team, list out all the players... also simple enough.

{{#each teams as |team index|}}
    <strong>{{team.name}}</strong></br>
    <em>Players</em>
    <ul>
    {{#each team.players as |player|}}
        <li>{{player.name}} - {{player.status}}</li>
    {{/each}}
    </ul>
{{/each}}

My QUESTION... let us say certain players are injured. If I'm looping through the teams, how would I go about ONLY displaying Teams that have injured players. If at least one player is injured, the TEAM will display, if not... the team will not display?

Upvotes: 0

Views: 185

Answers (2)

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9406

Please take a look at this twiddle

Upvotes: 1

Ember Freak
Ember Freak

Reputation: 12872

You can try filtering using isAny method for players

export default Ember.Route.extend({
    model() {
        return this.store.findAll('team').then((teams)=>{
            return teams.get('players').then((players) => {
                return players.isAny('status','injured');
            })
        })
    }
})

Upvotes: 2

Related Questions