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

╄→尐↘猪︶ㄣ 提交于 2019-12-05 12:40:55

Solved! If I just use #with instead of #if then everything works great. As it says in the handlebar docs You can shift the context for a section of a template by using the built-in with block helper.

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

The nested if/with is quite cumbersome and clearly not worth it for only rendering the name, but I could think of times where special rendering for more attributes of a single object would be useful.

I did a similar thing like this, but I don't think it is the "right way"?

Template.leaderboard.selected_person = function () {
    return Players.find(Session.get("selected_player"));
};

{{#each selected_person}}
<div class="details">
    <div class="name">{{name}}</div>
    <input type="button" class="inc" value="Give 5 points" />
</div>
{{/each}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!