0

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?

Matt
  • 1,811
  • 1
  • 19
  • 30

2 Answers2

2

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');
            })
        })
    }
})
Ember Freak
  • 12,918
  • 4
  • 24
  • 54
1

Please take a look at this twiddle

Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
  • with pulling data from a database, I'm having an issue getting it to render. In other words, if I load the record with all the relationships, I either needs to save the record (for it to show the filtered results) or in a beforeModal, load in the other has many records. Is there a way I can call the computed property later? or some other means? PS, thank you for the twiddle... very helpful! – Matt Mar 06 '17 at 01:00
  • @Matt I don't understand what is the issue! If you want to filter the team based on player (relationship) so you load relationship implicitly in the way ember data defined. Please get more info about the issue you mentioned – Ebrahim Pasbani Mar 06 '17 at 01:04
  • What I'm saying is, it seems as though the filter runs before the all the models load, since all of the model data is not there, it does not display the content. Is there a way to delay when the computed property runs, or tell it to 'wait' for all of the models to load first? – Matt Mar 06 '17 at 02:28
  • @Matt You're wrong. The computed property runs whenever `model` would be presented. I updated the twiddle to show this – Ebrahim Pasbani Mar 06 '17 at 02:41
  • Ok there must be something else at play then, thank you for the help. – Matt Mar 06 '17 at 02:45
  • @Matt You're welcome. If there is issue more existed, feel free to ask – Ebrahim Pasbani Mar 06 '17 at 02:48