2014-10-31 154 views
1

有沒有辦法獲得一個數組的索引在一個句柄模板中,我想獲得數組中的最後一個值,並調用一個。它Handlebars數組長度-1

{{currentRevision.computedRoutingNodes.length-1.numberOfReviewDays}} 

computedRoutingNodes屬性爲對象

我知道我能得到這樣的

{{currentRevision.computedRoutingNodes.1.numberOfReviewDays}} 

索引的數組,但我想最後一個值動態

+0

[在Handlebars模板中獲取JSON數組中的最後一個元素]的可能重複(http://stackoverflow.com/questions/10755092/getting-the-last-element-from-a-json-array-in -a-handlebars-template) – McGarnagle 2014-10-31 16:17:26

+0

@McGarnagle我認爲這是一種不同的操作,因爲操作系統正在尋找最後一個項目的屬性,而不是最後一個項目本身。看起來你應該能夠修改你發佈的代碼...... – 2014-10-31 16:18:43

回答

1

{{currentRevision.computedRoutingNodes.lastObject.numberOfReviewDays}}如果你使用Ember.js會工作。

1

你可以使用幫手:

Handlebars.registerHelper('propAtLengthRelativeIndex', function (arr, index, prop) { 
    return new Handlebars.SafeString(arr[arr.length + ~~index][prop]); 
}); 

然後調用它像這樣:

{{propAtLengthRelativeIndex currentRevision.computedRoutingNodes '-1' 'numberOfReviewDays'}}