How to get the previous item in {{#each}} loop


#1

Does anyone know how to get the previous item with-in an {{#each}} loop, or have any strategies for achieving this effect?


Strategy for creating a group messaging interface
#2

@index inside #each might be relevant:

You can use a special variable @index in the body of #each to get the 0-based index of the currently rendered value in the sequence.

combined with #let to access an element at index @index-1

Haven’t tried it though, but this is my best guess.


#3

I was not able to find a solution with-in Blaze itself, but have discovered that this can be worked around by querying minimongo directly using findOne, sort, and the required fields.

This works so long as you are sorting the documents with-in the {{#each}} statements. For example, if you are sorting the collection by number or date, you can use $lt or $gt to grab the previous or next document.

To minimize the amount of Trackers being spun up, one can use the fields property to specify exactly what is needed. This is probably a good practice for all Meteor apps.