if anyone completed or is following David Turnbull's 'Creating your first Meteor app' this is a question from that.
On page 102 it says to "Add a “Score” field to the “Add Player” form, allowing users to define a score for a player when they’re being submitted to the list."
I figured out how to do it, using this in my leaderboard.js file:
Template.addPlayerForm.events({
'submit form': function (event) {
event.preventDefault();
var playerNameVar = event.target.playerName.value;
var playerScoreVar = event.target.playerScore.value;
PlayersList.insert({
name: playerNameVar,
score: playerScoreVar
});
var playerNameVar = event.target.playerName.value = "";
var playerScoreVar = event.target.playerScore.value = "";
}
});
And in my leaderboard.html file:
<template name="addPlayerForm">
<form>
<input type="text" name="playerName">
<input type="number" name="playerScore">
<input type="submit" value="Add Player">
</form>
</template>
When a new player is created, the player is put at the top of the list regardless of their score. Which is sorted using this:
Template.leaderboard.helpers({
'player': function(){
return PlayersList.find({}, {sort: {score: -1, name: 1} });
// -1 sorts descending, while 1 will sort ascending
},
...
});
My guess is maybe I am not defining the score properly. Any help would be great!