Passing an array to linkTo helper

I want to create a component that should generate dynamic links. I tried passing the link data as an array, but this will not work.

var user1 = get("store").find("user", 1); var data = {link: ["users.show", user1], title: "User1"}; {{#link-to data.link}}{{data.title}}{{/link-to}} 

It should be equal

 {{#link-to "users.show" 1}}{{data.title}}{{/link-to}} 

How to create fully dynamic links from a variable?

+6
source share
3 answers

You can specify an array as an argument to params in the link-to helper. Like nickiaconis answer , but only with the default assistant {{link-to}} :

 {{#link-to params=data.link}}{{data.title}}{{/link-to}} 

... will do something like:

 <a href="/users/show/1">User1</a> 

(verified using Ember 2.3.0)

+6
source

Ember 1.13.x

The LinkComponent that the helper link creates for you is displayed as - link to . I created an example of its use here: http://emberjs.jsbin.com/rinukefuqe/2/edit?html,js,output

 {{#-link-to params=(unbound link) hasBlock="true"}} {{title}} {{/-link-to}} 

The params attribute is that helper binding usually binds your positional parameters, although you should use an unbound helper here because LinkComponent expects params to be an array , not a value binding object. In addition, the definition of using as a block or inline component is not yet built into the components , so you must pass hasBlock="true" if you do not include the link text as the first parameter in your array.


Amber ≤ 1.12.x

Although this has not yet been done, you can manually open the LinkView component, which is the equivalent of the new LinkComponent.

 App.XLinkToComponent = Ember.LinkView.extend(); 

Then use it like:

 {{#x-link-to params=link}} {{title}} {{/x-link-to}} 

Using unbound and hasBlock="true" optional since LinkView's internal logic is different from LinkComponent.

+3
source

I think it is not possible to pass an array, but you can pass each argument directlly, as shown below:

way

  var user1 = this.store.find('user', 1); var data = { data: { link: "users.show", model: user1, title: "User1" } }; return data; 

template

  {{#link-to data.link data.model.id}}{{data.title}}{{/link-to}} 

I hope this helps

0
source

Source: https://habr.com/ru/post/958274/


All Articles