vendredi 29 avril 2016

Ember: hasMany relationship doesn't always load on transitionTo

I think I'm missing something about how Ember Data works. I have a route that updates stats for the players on a team:

    export default Ember.Route.extend({
        stats: Ember.inject.service('constants'),
        actions: {
            submitStats(updates){
                let id = this.get('context').get('id');
                let team = this.store.findRecord('team', id);
                let players = team.then(t => t.get('players'));
                players.then(ps => savePlayers(ps, updates, this.get('stats')))
                       .then(p => {console.log("All saved!");
                                   this.transitionTo('team', team.get('id'))})
            }
        },

        init() {
            // we seemingly need to make sure all the players are loaded
            // before calling the component
            this.store.findAll('player');
        }
    });

The first time I update the stats, I transition to the team route and it all looks like I expect (and I see the stats for all players on the team). If I go submit stats a second time, I only see some of the players. Currently, my team route looks like the following:

export default Ember.Route.extend({
    beforeModel(transition){
        return this.store.findAll('player')
    },
    model(params){
        return this.store.findAll('player').then(p => {console.log("players loaded");
             return this.store.findRecord('team', params.team_id)})
    }
});

I'm assuming I'm missing... something about how models and transitions work together. What is it?




Aucun commentaire:

Enregistrer un commentaire