Skip to content

Instantly share code, notes, and snippets.

@ericwikman
Last active August 29, 2015 13:58
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 ericwikman/9997736 to your computer and use it in GitHub Desktop.
Save ericwikman/9997736 to your computer and use it in GitHub Desktop.
Games = new Meteor.Collection('games');
Games.allow({
update: ownsDocument,
remove: ownsDocument
});
Games.deny({
update: function(userId, game, fieldNames) {
// may only edit the following two fields:
return (_.without(fieldNames, 'awayTeamName', 'homeTeamName').length > 0);
}
});
Meteor.methods({
game: function(gameAttributes) {
var user = Meteor.user()
if (!user)
throw new Meteor.Error(401, "You need to login to create new games");
if (!gameAttributes.awayTeamName)
throw new Meteor.Error(422, 'Please fill in an away team');
if (!gameAttributes.homeTeamName)
throw new Meteor.Error(422, 'Please fill in an home team');
// pick out the whitelisted keys
var game = _.extend(_.pick(gameAttributes, 'awayTeamName', 'homeTeamName'), {
userId: user._id,
author: user.username,
submitted: new Date().getTime(),
currentFrame: 0
});
var gameId = Games.insert(game);
var teamOneId = Teams.insert({
gameId: gameId,
name: gameAttributes.awayTeam,
opponentName: gameAttributes.homeTeam,
score: 0,
stDev: 0,
projectedTotal: 0
});
var teamTwoId = Teams.insert({
gameId: gameId,
name: gameAttributes.awayTeam,
opponentId: teamOneId,
opponentName: gameAttributes.homeTeam,
score: 0,
stDev: 0,
projectedTotal: 0
});
return gameId;
}
});
Template.gameSubmit.events({
'submit form': function(e) {
e.preventDefault();
var game = {
awayTeamName: $(e.target).find('[name=awayTeamName]').val(),
homeTeamName: $(e.target).find('[name=homeTeamName]').val()
}
Meteor.call('game', game, function(error, id) {
if (error) {
// display the error to the user
throwError(error.reason);
if (error.error === 302)
Router.go('gamePage', {_id: error.details})
} else {
Router.go('gamePage', {_id: id});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment