Skip to content

Instantly share code, notes, and snippets.

@edward
Created April 11, 2012 05:45
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 edward/2357254 to your computer and use it in GitHub Desktop.
Save edward/2357254 to your computer and use it in GitHub Desktop.
Bug in Meteor client-side update
// See line 45 for the exception I'm trying to figure out
// Set up a collection to contain player information. On the server,
// it is backed by a MongoDB collection named "players."
Players = new Meteor.Collection("players");
function random_score () {
return Math.floor(Math.random()*10)*5;
}
if (Meteor.is_client) {
Template.leaderboard.sort_order = function () {
return Session.equals("sort_order", 'name') ? {name: 1, score: -1} : {score: -1, name: 1};
}
Template.leaderboard.players = function () {
return Players.find({}, {sort: Template.leaderboard.sort_order() });
};
Template.leaderboard.selected_name = function () {
var player = Players.findOne(Session.get("selected_player"));
return player && player.name;
};
Template.player.selected = function () {
return Session.equals("selected_player", this._id) ? "selected" : '';
};
Template.leaderboard.events = {
'click input.inc': function () {
Players.update(Session.get("selected_player"), {$inc: {score: 5}});
},
'click input.dec': function () {
Players.update(Session.get("selected_player"), {$inc: {score: -5}});
},
'click input.sort_by_name': function () {
Session.set("sort_order", 'name');
},
'click input.sort_by_score': function () {
Session.set("sort_order", 'score');
},
'click input.randomize_scores': function () {
Players.find().forEach(function (player) {
// Any idea why this throws the following exception?
// Exception while simulating the effect of invoking '/players/update' undefined
Players.update(player, {$set: {score: random_score}})
});
}
};
Template.player.events = {
'click': function () {
Session.set("selected_player", this._id);
}
};
}
// On server startup, create some players if the database is empty.
if (Meteor.is_server) {
Meteor.startup(function () {
if (Players.find().count() === 0) {
var names = ["Ada Lovelace",
"Grace Hopper",
"Marie Curie",
"Carl Friedrich Gauss",
"Nikola Tesla",
"Claude Shannon",
"Sherpa Sherpa"];
for (var i = 0; i < names.length; i++)
Players.insert({name: names[i], score: random_score()});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment