Skip to content

Instantly share code, notes, and snippets.

@mplatts
Last active Apr 10, 2016
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@tdamsma 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

This comment has been minimized.

Copy link
Owner Author

@mplatts 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