Team name integrity

Since we chose we denormalizing our data and duplicating our team names in each game, we need to make sure these stay up to date. Basically, whenever we modify a team name, we need to find all of that team's games and update the team name in them. You can have a go yourself - here's my code:

client/views/team.js

...
  "submit form.form-edit": function(e, tpl){
    e.preventDefault();

    var teamName = tpl.$("input[name='name']").val();
    var self = this;

    if(teamName.length){
      Teams.update(this._id, {$set: {name: teamName}}, function(error){

        if(!error){

          // Update games this team is a part of
          var games = Games.find({_id: {$in: self.gameIds}});
          if(games.count()){
            _(games.fetch()).each(function(game){
              var team = _(game.teams).findWhere({_id: self._id});
              if(team != null){
                team.name = teamName;
                Games.update({_id: game._id}, {$set: {teams: game.teams}})
              }
            });
          }
        }
      });

      Session.set('editedTeamId', null);
    }
  },

...

Here I have modified the team 'edit' event handler to update the team name in all its games ... but only after the team name has successfully been updated (I do it in the Teams.update() callback).

Underscore

One piece of the code from above that might be unfamiliar to you is this:

_(games.fetch()).each(function(game){ ... })

This is an Underscore function (Meteor provides Underscore to you by default). Underscore tries to fill in the gaps of Javascript; giving you functions like map, filter and each, which is what we've used here. Note that this could also be written as _.each(games.fetch(), function(game){ ... });.