pra*_*ase 4 string-interpolation handlebars.js ember.js
我正在使用http://ivaynberg.github.io/select2/在rails-emberjs应用程序中提供自动完成功能.我有some_dynamic_id值.我的车把代码如下:
{{
view App.TokenAutoCompleteView
searchUrl="/users/" + some_dynamic_id + "/books/search"
optionValuePath="content.id"
optionLabelPath="content.name"
multiple="false"
}}
Run Code Online (Sandbox Code Playgroud)
我想为searchUrl属性分配一个动态创建的URL.我尝试在车把模板的href标签中使用来自Ember Interpolation的 bind-attr,但无法使其正常工作.
请记住,Handlebars模板是无逻辑的,因此没有字符串插值的概念.您应该在控制器(或组件)内部的计算属性中执行此操作.
在你的控制器中:
dynamicSearchUrl: function() {
return '/users/' + this.get('someDynamicId') + '/books/search';
}.property('someDynamicId')
Run Code Online (Sandbox Code Playgroud)
在您的模板中:
{{view App.TokenAutoCompleteView searchUrl=dynamicSearchUrl ... }}
Run Code Online (Sandbox Code Playgroud)
更新:自从我回答这个问题已经很久了,所以我想我会更新.Ember.js现在使用支持子表达式的更新版Handlebars .这意味着现在可以在模板中进行字符串连接.你只需要编写一个连接帮助器.
// helpers/concat.js
export default Ember.Helper.helper(function(first, second, three) {
// You could make this apply to more than three strings...
return first + '' + second + '' + three;
});
Run Code Online (Sandbox Code Playgroud)
完成后,只需在模板中使用子表达式:
{{
view App.TokenAutoCompleteView
searchUrl=(concat "/users/" some_dynamic_id "/books/search")
optionValuePath="content.id"
optionLabelPath="content.name"
multiple="false"
}}
Run Code Online (Sandbox Code Playgroud)
在这种情况下我仍然非常喜欢我的第一个答案,但现在第二个答案是可能的.
| 归档时间: |
|
| 查看次数: |
4869 次 |
| 最近记录: |