问题
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