如何使用动态 ID 访问车把中关联数组的元素?
How can I access an element of an associative Array in handlebars with dynamic id?
我目前正在尝试访问 emberjs handlebars 文件中的关联数组。通过硬编码值访问没有问题,但我想获取基于动态 "index".
的元素
我目前的代码是
{{#each this.model.ambient as |sound|}}
<Ambientsound @buttonActive={{soundPlayers.[Bells]}} @sound={{sound}} @updateFunction={{action "updateSound"}}/>
{{/each}}
控制器的相关部分是:
soundPlayers: {"Bells":100,"Fireplace":30}
我上面的代码工作得很好,但现在我正在尝试访问 soundPlayers。[...] 基于 sound.id 的值,像这样:
{{#each this.model.ambient as |sound|}}
<Ambientsound @buttonActive={{soundPlayers.[sound.id]}} @sound={{sound}} @updateFunction={{action "updateSound"}}/>
{{/each}}
sound.id 中的值作为字符串传递,对此我无能为力。
如何将 sound.id 转换为允许我访问数组的内容,或者以某种方式对 handlebars 查询进行短语处理以便从数组中获取结果?我还可以考虑重塑控制器中的数组。欢迎提供任何帮助!
首先你应该做 {{soundPlayers.Bells}}
。
接下来你可以使用 get
助手:
<Ambientsound @buttonActive={{get soundPlayers sound.id}} @sound={{sound}} @updateFunction={{action "updateSound"}}/>
我目前正在尝试访问 emberjs handlebars 文件中的关联数组。通过硬编码值访问没有问题,但我想获取基于动态 "index".
的元素我目前的代码是
{{#each this.model.ambient as |sound|}}
<Ambientsound @buttonActive={{soundPlayers.[Bells]}} @sound={{sound}} @updateFunction={{action "updateSound"}}/>
{{/each}}
控制器的相关部分是:
soundPlayers: {"Bells":100,"Fireplace":30}
我上面的代码工作得很好,但现在我正在尝试访问 soundPlayers。[...] 基于 sound.id 的值,像这样:
{{#each this.model.ambient as |sound|}}
<Ambientsound @buttonActive={{soundPlayers.[sound.id]}} @sound={{sound}} @updateFunction={{action "updateSound"}}/>
{{/each}}
sound.id 中的值作为字符串传递,对此我无能为力。
如何将 sound.id 转换为允许我访问数组的内容,或者以某种方式对 handlebars 查询进行短语处理以便从数组中获取结果?我还可以考虑重塑控制器中的数组。欢迎提供任何帮助!
首先你应该做 {{soundPlayers.Bells}}
。
接下来你可以使用 get
助手:
<Ambientsound @buttonActive={{get soundPlayers sound.id}} @sound={{sound}} @updateFunction={{action "updateSound"}}/>