meteorBuzz
meteorBuzz

Reputation: 3200

How can I reverse the ordering of spacebars #each block

A helper sentencesList is an array of objects containing an element text.

HTML:

{{#each sentencesList}}
  {{text}}
{{/each}}

CLIENT.JS

sentencesList: function() {
    return Session.get(SENTENCES)
}

How can I reverse the ordering, i.e the highest index number is shows at the top and the element at index position 0 is at the bottom?

Upvotes: 3

Views: 3307

Answers (2)

Little Brain
Little Brain

Reputation: 2855

If you want to reverse more than one array, you could create a generic helper in (say) main.js:

Template.registerHelper('reverseArray', (array) => array.reverse());

In your template:

{{#each (reverseArray sentencesList)}}
  {{text}}
{{/each}}

EDIT: Updated code to include parentheses as per Pierre's comment

Upvotes: 3

SylvainB
SylvainB

Reputation: 4820

You could just reverse the array:

sentencesList: function() {
    return Session.get(SENTENCES).reverse();
}

Please note: although {{#each}} works on both arrays and cursors, this method is array-specific. So you cannot use it, say, on the return of a Collection.find() call. To achieve this with a collection, you will have to use sort on the key you want to reverse order from.

Upvotes: 8

Related Questions