Modelling a tennis match. Can parent Routes observe changes made by child Routes?


#1

I have an Ember where I can keep scores. It’s got heavily nested routes, from Match -> Set -> Game -> Point

I guess my question is: can a Route observe changes in its child’s models and act upon that?

I would like for instance for the Game route to notice that the winner for a certain Point (a hasMany child of its model “Game”) has changed (from null to someone) and than act on that. The “Game” route can then decide if it should set the winner of its model (a Game), which in turn will be noticed by the “Set” route, etc etc.


#2

I’ve solved it using bubbling action.

From my PointsRoute (leaf node) I call:

this.send('registerPointWin', point);

This gets sent up the Route tree where the GameRoute will handle it:

  actions: {
    registerPointWin: function (point) {
      const game = this.get('controller.model');
      const calculatedWinner = game.get('calculatedWinner');

      if (calculatedWinner && calculatedWinner.get('id') !== game.get("winner.id")) {
        game.set('winner', calculatedWinner);
        this.send("registerGameWin", game)
      } else {
        this.transitionTo('matches.new.set.game.point', parseInt(point.get("nr")) + 1)
      }
    }
  }