2013-10-28 30 views
16

访问对象值寻找一种方式来获得实现这一点:Handlebars.js - 一个可变密钥

{{#each someArray}} 
    {{../otherObject.[this]}} 
{{/each}} 

如何评估的this值,然后引用它作为重点,以我的对象otherObject ?与助手

+0

阅读侧边栏中的相关问题并进行一些搜索,我确定这是一个重复的问题。 –

+4

我有,搜索了2天。也许我只是在使用谷歌搜索。 –

+1

[This side from the sidebar](http://stackoverflow.com/questions/18168924/how-to-get-an-array-value-at-index-using-handlebars-js?rq=1)约为90解决方案的百分比。基本上,您必须编写自己的帮助器,将诸如'{{value_at obj key}}'之类的东西转换为JavaScript'obj [key]'。 –

回答

5

一个可能的解决方案:

/* 
{{#each someArrayOfKeys}} 
    {{#withItem ../otherObject key=this}} 
    {{this}} 
    {{/withItem}} 
{{/each}} 
*/ 

Handlebars.registerHelper('withItem', function(object, options) { 
    return options.fn(object[options.hash.key]); 
}); 
0

正如其他答案已经指出的那样,你可能想lookup

但是,如果您使用EmberJS(在撰写本文时为2.12.0),则需要get