{{#link-to ‘routeName’}} with dynamic context

半世苍凉 提交于 2019-12-24 12:08:30

问题


Is it possible to use link-to with dynamic context params as a single variable?

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.

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.


回答1:


No, not as a single variable. You can, however, achieve your goal (if I understand it correctly) by passing in both values under the same object.

So in your example, you'd have to change your params property to something like this:

params: Em.computed(function() {
  return { params: { catId: 1, postId: 4 } };
})

And then pass in the params to the full path as two dynamic properties:

{{#link-to "post-categories.post" params.catId params.postId}}



回答2:


No, it would consider that the model to be used for one of the routes. Your best bet is to use two dynamic properties.

{{link-to 'My Post' 'post' p1 p2}}



回答3:


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


来源:https://stackoverflow.com/questions/27929066/link-to-routename-with-dynamic-context

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!