打开一个流星方法,将单个对象返回到句柄的上下文中 [英] Turn a meteor method returning a single object into a context for handlebar

查看:116
本文介绍了打开一个流星方法,将单个对象返回到句柄的上下文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Template.leaderboard.selected_name = function 

在meteor.com的基本排行榜示例中,有一个名为selected_name的方法。 (){
var player = Players.findOne(Session.get(selected_player));
回报玩家&&参赛者姓名;
};

{{#if selected_name}}
< div class =details>
< div class =name> {{selected_name}}< / div>
< input type =buttonclass =incvalue =Give 5 points/>
< / div>
{{/ if}}

相反,我想返回整个玩家对象和然后通过把手将该对象视为上下文。我希望我可以这样说:

  Template.leaderboard.selected_person = function(){
var player = Players。 findOne(Session.get( selected_player));
返回玩家||假;
};

{{#if selected_person}}
< div class =details>
< div class =name> {{name}}< / div>
< input type =buttonclass =incvalue =Give 5 points/>
< / div>
{{/ if}}

上面的#if块实际上并不适用于流星。 #if语句只是对selected_person方法进行评估,嵌套的{{name}}完全没有。我想知道是否可以编写一个方法,以便返回的对象可以用作#if块的上下文。

解决方案

DIV>

解决了!如果我只是使用#with而不是#if,那么一切都很好。正如它在句柄docs 中所说的那样,您可以通过使用内置的块助手来移动模板的一部分的上下文。

  {{#if selected_person}} 
{{#with selected_person}}
< div class =details>
< div class =name> {{name}}< / div>
< input type =buttonclass =incvalue =Give 5 points/>
< / div>
{{/ with}}
{{/ if}}

嵌套的if / with非常麻烦,显然不值得为只渲染名称,但我可以想到,对于单个对象的更多属性进行特殊渲染将是有用的。


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.

解决方案

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.

这篇关于打开一个流星方法,将单个对象返回到句柄的上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆