<OUTDATED> about {{linkTo}} in Ember.js Guide

China☆狼群 提交于 2019-12-05 23:19:52

问题


I'm reading guide of Ember.js templates. In the handlebar part of the first example of above link, they used

{{#linkTo posts.post post}}

but I thought

{{#linkTo posts.post}}

would work.

Why do I need second argument 'post'?

I read the explanation:

If the route has a dynamic segment, a model that represents the segment. By default, Ember.js will replace the segment with the value of the object's id property.

but I can't associate this explanation with question above.


回答1:


The #linkTo helper takes three parameters.

  1. The route, in your case posts.post
  2. The context, here it is the particular post object you are referring to in the loop
  3. An options hash (currently title is supported)

Because the posts.post route is a dynamic route, e.g. it can be for one of a collection of posts, we must provide the #linkTo helper with which particular post you are wanting to link to.




回答2:


What the other people said in terms of the arguments passed to the #link-to helper in Ember is accurate here is the link to the documentation for anyone that is looking for more info.

You have the route name that you are linking to and the second argument is providing the model context.

Here is more info:

http://emberjs.com/api/classes/Ember.Templates.helpers.html#method_link-to




回答3:


For me code below works fine:

  {{#each model as |book|}}
      <h3>{{#link-to 'book' book}}{{book.title}}{{/link-to}}</h3>
  {{/each}}



回答4:


Link-to has format of inline and block

Reference: EmberJS-Template Link To Inline Helper



来源:https://stackoverflow.com/questions/14453976/outdated-about-linkto-in-ember-js-guide

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