boblikesoup
boblikesoup

Reputation: 302

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.

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. 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 ++/--. 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. Appreciate any help.

Upvotes: 1

Views: 141

Answers (1)

Philip Pryde
Philip Pryde

Reputation: 940

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:

<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>

Upvotes: 3

Related Questions