在 handlebars 中使用 #each 循环时访问先前的索引值
Accessing previous index value while looping with #each in handlebars
在我的 handlebars 应用程序中,我试图遍历 post 的集合。我正在尝试访问每个 post 的 ID。我正在正确获取当前 post 的 ID。但是对于每张图片,我都需要提供 Previous 和 Next id。我使用数学助手将索引 increment/decrement 加 1,它工作正常。
我的想法是,我将使用 handlebars 子表达式来计算所需的索引,然后我将使用查找助手来访问该索引处的值。但是我的代码没有按预期工作。有人可以帮我找出正确的代码吗?
{{#each posts}}
<div class="lb-overlay" id="image-{{id}}">
<a href="#image-{{lookup id (math @index '-' 1)}}" class="lb-prev">Prev</a>
<a href="#image-{{lookup id (math @index '+' 1)}}" class="lb-next">Next</a>
</div>
{{/each}}
更新:我自己想出来了。实现了我自己的车把助手。
{{#each posts}}
<div class="lb-overlay" id="image-{{id}}">
<a href="#image-{{getIdByIndex ../posts (math @index '-' 1)}}" class="lb-prev">Prev</a>
a href="#image-{{getIdByIndex ../posts (math @index '+' 1)}}" class="lb-next">Next
</div>
{{/each}}
{{#each posts}}
<div class="lb-overlay" id="image-{{id}}">
<a href="#image-{{getIdByIndex ../posts (math @index '-' 1)}}" class="lb-prev">Prev</a>
a href="#image-{{getIdByIndex ../posts (math @index '+' 1)}}" class="lb-next">Next
</div>
{{/each}}
getIdByIndex: function(posts, index) {
return posts[index].id;
}
在我的 handlebars 应用程序中,我试图遍历 post 的集合。我正在尝试访问每个 post 的 ID。我正在正确获取当前 post 的 ID。但是对于每张图片,我都需要提供 Previous 和 Next id。我使用数学助手将索引 increment/decrement 加 1,它工作正常。
我的想法是,我将使用 handlebars 子表达式来计算所需的索引,然后我将使用查找助手来访问该索引处的值。但是我的代码没有按预期工作。有人可以帮我找出正确的代码吗?
{{#each posts}}
<div class="lb-overlay" id="image-{{id}}">
<a href="#image-{{lookup id (math @index '-' 1)}}" class="lb-prev">Prev</a>
<a href="#image-{{lookup id (math @index '+' 1)}}" class="lb-next">Next</a>
</div>
{{/each}}
更新:我自己想出来了。实现了我自己的车把助手。
{{#each posts}}
<div class="lb-overlay" id="image-{{id}}">
<a href="#image-{{getIdByIndex ../posts (math @index '-' 1)}}" class="lb-prev">Prev</a>
a href="#image-{{getIdByIndex ../posts (math @index '+' 1)}}" class="lb-next">Next
</div>
{{/each}}
{{#each posts}}
<div class="lb-overlay" id="image-{{id}}">
<a href="#image-{{getIdByIndex ../posts (math @index '-' 1)}}" class="lb-prev">Prev</a>
a href="#image-{{getIdByIndex ../posts (math @index '+' 1)}}" class="lb-next">Next
</div>
{{/each}}
getIdByIndex: function(posts, index) {
return posts[index].id;
}