EmberJS - 如何用linkTo动态生成链接? [英] EmberJS - How to dynamically generate link with linkTo?

查看:61
本文介绍了EmberJS - 如何用linkTo动态生成链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 动态生成链接链接到 帮助者通过路由路径传递变量?

Is there a way to dynamically generate a link using the link-to helper by passing a variable with the route path?

例如,而不是硬编码这样的路径:

For example, instead of hard coding the path like this:

{{#linkTo "route.subroute" model}}{{model.title}}{{/linkTo}}

我想要能够传递 link-to 帮助一个保存路径的变量:

I want to be able to pass the link-to helper a variable which holds the path like this:

{{#linkTo destination model}}{{model.title}}{{/linkTo}}

这个用例是让我合并只有这个路径不同的模板。例如,如果有两个不同目的地的集合。当与每个帮助者呈现时,模板是相同的,除了 <$ c中的路径$ c> link-to

The use case for this is to allow me to consolidate templates which only differ by this path. For instance, if there are two collections each with a different destination. When rendered with the each helper the templates are the same except for the path in the link-to.

如果我可以将该变量保存在视图的控制器中并传递,我可以使用

If I could save this variable in the view’s controller and pass it, I could use only one template for both the lists.

我已经考虑过如下手动构建路径:

I have thought about manually constructing the path like this:

<a {{bindAttr href="destination"}}>{{model.title}}</a>

但是,这并没有与路由器的集成来确定我需要的活动状态。 p>

But this does not have the integration with the router to determine the active state which I need.

推荐答案


有没有办法使用linkTo帮助器动态生成链接,通过传递一个变量与路由路径?

Is there a way to dynamically generate a link using the linkTo helper by passing a variable with the route path?

目前不在。


这样的用例是让我整合只有这个路径不同的模板。例如,如果有两个不同目的地的集合。

The use case for this is to allow me to consolidate templates which only differ by this path. For instance, if there are two collections each with a different destination.

同意这是一个有效的用例,我期望linkTo帮助将来支持它。同时,由于您只有两个集合,您可以使用模板中的条件来完成此任务

Agreed this is a valid use case, I would expect linkTo helper to support it in future. Meantime since you've only got two collections you could accomplish this with conditionals in the template

{{#if isRouteOne}}
  {{#linkTo "routeOne.subrouteOne" model}}{{model.title}}{{/linkTo}}
{{else}}
  {{#linkTo "routeTwo.subrouteTwo" model}}{{model.title}}{{/linkTo}}
{{/if}}

这篇关于EmberJS - 如何用linkTo动态生成链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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