{{#link-to'routeName'}}与动态上下文 [英] {{#link-to ‘routeName’}} with dynamic context
问题描述
可以将链接到动态上下文参数作为单个变量使用吗?
Is it possible to use link-to with dynamic context params as a single variable?
当有1个动态部分时,这可以工作,但在下面的示例中 post-category-id
和 post-id
。是否可以动态地传递这些参数。它尝试将它们作为数组或String传递,但这不起作用。
This works when there is 1 dynamic part, but in the example below i have post-category-id
and post-id
. Is it possible to pass these params in dynamically. It tried to pass them as array or as a String, but this is not working.
在示例中,我想要 {{link- postparams}}
其中帖子嵌套在后类别中。我的目标是通过所需的2个上下文参数传递参数。
In the example I want to {{#link-to "post" params}}
where post is nested in post-category. My goal is to pass in params with the 2 context params needed.
// router
this.resource('post-categories', {path: 'post-categories/:post-category-id'}, function() {
this.resource('post', {path: 'post/:post-id'})
});
// controller
...
params: Em.computed(function() {
return [1, 4]
})
...
// template
{{#link-to "post" params}}
我尝试在源代码中阅读,但这并没有太大帮助。
I tried to read in the source, but that didn't help too much.
推荐答案
好的,我的解决方案通过控制器上的动作。
在此操作中,我检查我的参数的长度,并切换到 transitionToRoute
,并具有所需的动态部件数量。虽然不是刚刚干了。
Okay, my solution goes through an action on the controller.
In this action I check the length of my params and switch case transitionToRoute
with the desired amount of dynamic parts. Though not verry DRY.
switch (params.length) {
case 0:
this.transitionToRoute(route.get('routeName'))
break;
case 1:
this.transitionToRoute(route.get('routeName'), params[0])
break;
case 2:
this.transitionToRoute(route.get('routeName'), params[0], params[1])
break;
case 3:
this.transitionToRoute(route.get('routeName'), params[0], params[1], params[2])
}
这篇关于{{#link-to'routeName'}}与动态上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!