Skip to content

Instantly share code, notes, and snippets.

@nashibao
Created April 16, 2012 09:05
Show Gist options
  • Save nashibao/2397186 to your computer and use it in GitHub Desktop.
Save nashibao/2397186 to your computer and use it in GitHub Desktop.
Meteor Sample(Leaderboard) in CoffeeScript
Players = new Meteor.Collection("players")
if Meteor.is_client
Template.leaderboard.players = () ->
return Players.find {}, {sort: {score: -1, name: 1}}
Template.leaderboard.selected_name = () ->
player = Players.findOne(Session.get("selected_player"));
return player and player.name
Template.player.selected = () ->
return if Session.equals("selected_player", this._id) then "selected" else ''
Template.leaderboard.events =
'click input.inc': () ->
Players.update(Session.get("selected_player"), {$inc: {score: 5}})
Template.player.events =
'click': () ->
Session.set("selected_player", this._id)
if Meteor.is_server
Meteor.startup () ->
if Players.find().count() is 0
names = ["test1", "test2", "test3"]
for name in names
Players.insert({name: name, score: Math.floor(Math.random()*10)*5})
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment