following emberjs guide for #linkTo helper does not display individual post

主宰稳场 提交于 2019-12-24 20:40:42

问题


I have a jsfiddle with a route as shown below, which is exactly thesame as the one in emberjs guides and when I click on the #linkTo helper attached to {{post.title}}, it ought to show me the individual post but it does not and instead the console shows this errors:

Uncaught Error: assertion failed: Cannot call get with 'id' on an undefined object.

Also when I click the posts link on the home page, it displays all the titles but in the console, it also shows this error:

Uncaught Error: Something you did caused a view to re-render after it rendered but before it was inserted into the DOM.

EmBlog.Router.map(function() {
  this.resource("posts", function(){
    this.route('show', {path: '/:post_id'}) ;
  });
});

The template which is thesame as the emberjs guides

<script type="text/x-handlebars" data-template-name="posts/index">
  {{#each post in content}}
    <p>{{#linkTo 'posts.show' post}} {{post.title}}  {{/linkTo}}</p>
 {{/each}}
</script>

I looked at this commit that added support for string literals as param for {{linkTo}} and in particular the suggestions below from that commit:

Now, Ember allows you to specify string literals as arguments. {{#linkTo post popular}} would look up the "popular" property on the current context and generate a URL pointing to the model with that ID. While {#linkTo post "popular"}} would treat the string literal "popular" as the model.


回答1:


It seems like the only issue was you forget to pass the context to the linkTo posts.edit helper in the posts/show template.

<script type="text/x-handlebars" data-template-name="posts/show">
  <h1>Post</h1>
  <p>Your content here.</p>
  <h3> {{title}} </h3>
  <h3> {{body}} </h3>
  <br/>
  <p> {{#linkTo 'posts.index'}} back {{/linkTo}}</p>
  <p> {{#linkTo 'posts.edit' content}} Edit the post  {{/linkTo}}</p>  
</script>

Here is a working fiddle, BTW I've cleaned up a bit, some things seemed useless for me.

http://jsfiddle.net/rxWzu/9/



来源:https://stackoverflow.com/questions/14426730/following-emberjs-guide-for-linkto-helper-does-not-display-individual-post

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