简体   繁体   English

每个Blaze空格键-预览下一个和上一个项目

[英]Blaze Spacebars Each - Preview next and previous items

I have a helper returning an array of objects called game. 我有一个帮手,返回一个称为游戏的对象数组。 In spacebars I use: each game 在空格键中,我使用:每个游戏

I also have session for a selectedGame that is clicked by the user. 我也有一个由用户单击的selectedGame的会话。

In the template, I'd like to show properties for the next and previous games in the array. 在模板中,我想显示数组中下一个和上一个游戏的属性。 How can I do this? 我怎样才能做到这一点?

As of now what seems like it would work is setting the game array to a Session gameArray. 到目前为止,将游戏数组设置为Session gameArray似乎可行。 Then look up the selectedGame's index by id of the objects in the Session array, then using nextGame and previousGame helpers to access those which query that array by index ++/--. 然后通过Session数组中对象的ID查找selectedGame的索引,然后使用nextGame和previousGame帮助器访问通过索引++ /-查询该数组的对象。 This seems extremely obtuse, and I'm also getting an error about argument size while trying to set the Session and don't want to do multiple db calls. 这似乎很钝,而且在尝试设置Session且不想执行多个数据库调用时,我也遇到了有关参数大小的错误。 Appreciate any help. 感谢任何帮助。

If you are using Meteor >= 1.2 you can utilize the index function in some manner from your each and pass it through to your each template as context: 如果您使用的是Meteor> = 1.2,则可以以某种方式使用索引函数,并将其作为上下文传递给每个模板:

<template name="main">
  {{#each iterateMe}}
    {{> iteratee i=@index}}
  {{/each}}
</template>

Then you can register a helper: 然后,您可以注册一个助手:

Handlebars.registerHelper('isNextOrPrev', function(val){
  var x = Session.get("selectedGame");
  if (!!val && typeof val == "number" && (val == (x-1) || val == (x+1)) )
    return true;
  return false;
});

and check the helper in the template 并检查模板中的助手

<template name="iteratee">
  {{#if isNextOrPrev i}}
    <!--SHOW ADDITIONAL PROPERTIES HERE-->
  {{/if}}
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM