在 DerbyJS 中使用 {{each}} 块向后遍历数组

Using {{each}} block to iterate through an array backwards in DerbyJS

在 DerbyJS 组件中,我有一个数组 items,我想在视图中显示它。我想首先显示数组的最新项目,所以我正在寻找类似 each-reverse:

的东西
{{each-reverse items as #item}}
  <p>{{#item}}</p>
{{/each-reverse}}

。当然,这段代码不起作用,但我希望它能解释我想要实现的目标。有没有办法在 DerbyJS 中做到这一点?

理论上我可以反转 items 数组,但我不想这样做,因为有时我会使用 .push 更新这个数组。使用 .unshift 似乎不是最佳选择。

我很确定没有可以在视图中使用的 each-reverse 方法。实现您想要的一种方法是首先对控制器中的列表进行反向排序。

如果您使用 Derby 排序过滤器来反转数组中的项目,您仍然可以根据需要使用 push 来更新原始数组。

@originalList = @model.at 'originalList'
@reversedList = @model.ref 'reversedList',  @model.sort 'originalList', (a, b) -> 
    return a - b

// add new items to the list
@originalList.push 4

// in the view
{{each reversedList as #item}}
    <p>{{#item}}</p>
{{/each}}

或者,您可以尝试

// in the view
{{each originalList as #item, #index}}
 {{originalList[originalList.length - #index]}}
{{/each}}