如何使链接到动态的ember? [英] How to make linkTo dynamic in ember?
问题描述
<script type="text/x-handlebars" data-template-name="patient">
<ul class="nav">
{{#each menuItem in menuItems}}
<li>{{#linkTo "dashboard.summary" menuItem}}{{menuItem.name}}{{/linkTo}}</li>
{{/each}}
</ul>
{{outlet}}
</script>
在上面的代码中,如何使 linkTo
动态链接而不是硬编码的dashboard.summary?例如,dashboard。+ menuItem.name
。
In the above code, how do I make linkTo
a dynamic link instead of the hardcoded "dashboard.summary"? For example, "dashboard."+menuItem.name
.
推荐答案
您可以注册一个简单的Handlebars助手,其中包含 linkTo
帮助器。
You could register a simple Handlebars helper that wraps the linkTo
helper.
var linkTo = Ember.Handlebars.helpers.linkTo;
Ember.Handlebars.registerHelper('myLinkTo', function(name, suffixPath) {
var suffix = Ember.Handlebars.get(this, suffixPath);
arguments = [].slice.call(arguments, 2);
arguments.unshift(name + '.' + suffix);
return linkTo.apply(this, arguments);
});
然后在您的模板中,您可以写:
Then in your template you could write:
{{#each menuItems}}
<li>{{#myLinkTo "dashboard" name this}}{{name}}{{/myLinkTo}}</li>
{{/each}}
帮助者将解析第二个参数并将其附加到第一个,前面有一个点。
The helper will resolve the second argument and append it to the first, preceded by a dot.
编辑:现在可以在没有自定义帮助器的情况下实现此行为。有关此问题的当代解决方案,请参阅 c4p的答案。上述解决方案最后使用Ember 1.0.0-rc.1进行测试。
this behaviour can now be achieved without a custom helper. See c4p's answer for the contemporary solution to this problem. The solution above was last tested with Ember 1.0.0-rc.1.
这篇关于如何使链接到动态的ember?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!