0

So im doing the leaderboard example on the meteor site but instead of the predefined data I start off with, I want to create a new name and score that automatically appears on the screen when someone creates an account, so at this point I get the name and the score on the screen only after I create an account and hit the refresh button on the browser, what do I want to do so that I don't have to hit the refresh button and the user login name and score automatically appears on the screen?

do I want to use deps.flush() or meteor.render somehow?

server.js

// newUser Method

 Meteor.methods({
  newUser: function() {
   var user = Meteor.user();

    userVar = {
     name: user.username,
    score: 0
     };

   Players.insert(userVar);
     }
   });

client.js

Deps.autorun(function() {
  Meteor.call('newUser');
  });

 Template.leaderboard.players = function () {
   return Players.find({}, {sort: {score: -1, name: 1}});
  };

  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}});
   }
 });

Template.player.events({
 'click': function () {
  Session.set("selected_player", this._id);
 }
 });
2
  • Are you using one of the accounts packages (such as "accounts-base" )? Where is the code which triggers the user creation so that onCreateUser will be run?
    – user728291
    Jul 28, 2013 at 23:54
  • yes im using accounts-ui and accounts-password, I refactored the question and am now wondering how to automatically redraw the DOM without having to refresh the page
    – flylib
    Jul 29, 2013 at 0:06

2 Answers 2

1

If your starting point is a working version of the example then you should be seeing reactive changes to the web page each time the Players collection changes. Deps.flush or Meteor.render are unnecessary.

The Deps.autorun() function you have is only called once when the client starts. At that point you may not have a user and your method will fail when you try to get a username from the null variable, 'user'.

To trigger the autorun each login and when you have a user you need it to refer to a reactive data source. If you rewrite it like this you should see a new player showing up each time a user logs in:

//on client
Deps.autorun( function(){
  if ( Meteor.userId() ){
    Meteor.call('newUser');
  }
});

I also wonder if your method on the server will have a problem as this.userId is how I usually get the user information inside a method. Here is an alternative to avoid the method and just insert the player on the client:

//on client
Deps.autorun( function(){
  var user = Meteor.user();

  if ( user ) {   //insert will run on login or any change in the user
    var userVar = {
      name: user.username,
      score: 0
    };

    Players.insert(userVar);
  }
});
2
  • ok thanks for the help, I do the first one and it works fine except when I refresh the page it will create the same user over again so i will have two of the username on the screen and on the bottom suggestion, it works fine but then when I refresh the screen my screen comes up still logged in but blank
    – flylib
    Jul 29, 2013 at 1:09
  • good stuff. Yes, it repeats each login so if you only want each user once you can check db for it before inserting.
    – user728291
    Jul 29, 2013 at 2:34
1

So I assume then that "player" records belong to a user in some way? So when you create a new user, you create their new default player record?

Maybe you just need your helpers to check that a player record for the user exists, and if not, create it.

Template.leaderboard.players = function () {
  var players = Players.find({/* Get players for this user */ }, {sort: {score: -1, name: 1}});
  if(!players) {
    players = /* Insert default player record */
  }
  return players;
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.