Skip to content

Instantly share code, notes, and snippets.

@mplatts
Last active April 10, 2016 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mplatts/fb19cf82bb3b98285283 to your computer and use it in GitHub Desktop.
Save mplatts/fb19cf82bb3b98285283 to your computer and use it in GitHub Desktop.
templates2 - games.coffee
# 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
@tdamsma
Copy link

tdamsma commented Apr 12, 2015

Perhaps end Template.games.events with
Session.set creating-game', null
so the template switches back to create after submit

@mplatts
Copy link
Author

mplatts commented Apr 10, 2016

@tdamsma thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment