Monday, April 16, 2012

Turn a meteor method returning a single object into a context for handlebar

In the basic leaderboard example on meteor.com there is a method called selected_name.



  Template.leaderboard.selected_name = function () {
var player = Players.findOne(Session.get("selected_player"));
return player && player.name;
};

{{#if selected_name}}
<div class="details">
<div class="name">{{selected_name}}</div>
<input type="button" class="inc" value="Give 5 points" />
</div>
{{/if}}


Instead I would like to return the entire player object and then have that object be treated as a context by handlebar. I wish I could say this:



  Template.leaderboard.selected_person = function () {
var player = Players.findOne(Session.get("selected_player"));
return player || false;
};

{{#if selected_person}}
<div class="details">
<div class="name">{{name}}</div>
<input type="button" class="inc" value="Give 5 points" />
</div>
{{/if}}


The #if block above does not actually work in meteor. The #if statement is just evaluating the selected_person method and the nested {{name}} does absolutely nothing. I would like to know if it is possible to write a method so that the returned object can be used as the context of an #if block.





No comments:

Post a Comment