Last active
April 10, 2016 20:31
-
-
Save mplatts/fb19cf82bb3b98285283 to your computer and use it in GitHub Desktop.
templates2 - games.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# client/views/games.coffee | |
Template.games.helpers | |
teams: -> Teams.find() | |
games: -> Games.find() | |
creating: -> Session.get 'creating-game' | |
Template.games.events | |
"click .create": (e, tpl) -> | |
e.preventDefault() | |
Session.set 'creating-game', true | |
"click .cancel": (e, tpl) -> | |
e.preventDefault() | |
Session.set 'creating-game', null | |
"submit form.form-create": (e, tpl) -> | |
e.preventDefault() | |
teamOneData = { | |
id: tpl.$("select[name='teamOne']").val() | |
name: tpl.$("select[name='teamOne'] option:selected").text() | |
score: 0 | |
} | |
teamTwoData = { | |
id: tpl.$("select[name='teamTwo']").val() | |
name: tpl.$("select[name='teamTwo'] option:selected").text() | |
score: 0 | |
} | |
game = { | |
created_at: new Date() | |
teams: [teamOneData, teamTwoData] | |
completed: false | |
} | |
gameId = Games.insert(game) | |
# Update each team's cached array of game ids | |
Teams.update({_id: teamOneData.id}, {$addToSet: { games: gameId}}) | |
Teams.update({_id: teamTwoData.id}, {$addToSet: { games: gameId}}) | |
Session.set 'creating-game', null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perhaps end Template.games.events with
Session.set creating-game', null
so the template switches back to create after submit