如何在 ember 中使 linkTo 动态化? [英] How to make linkTo dynamic in ember?

查看:22
本文介绍了如何在 ember 中使 linkTo 动态化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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}}

helper 将解析第二个参数并将其附加到第一个参数后,前面有一个点.

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 中使 linkTo 动态化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆