While running through the starter tutorial on EmberJS' site, a few things have me a little confused now.
One thing to note immediately is that I decided to use the ember 1.9.0beta4 with handlebars 2.0.0 instead of 1.8.1 / 1.3.0 included in the starter pack.
First the code included in the screencast:
app.js
App.Router.map(function() {
this.resource('about');
this.resource('posts');
this.resource('post', {path: ':post_id'})
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return posts;
}
});
and
index.html
{{#each model}}
<tr><td>
{{#link-to 'post' this}}
{{title}} <small class='muted'>by {{author.name}}</small>
{{/link-to}}
</td></tr>
{{/each}}
This works exactly as expected and the requested post appears when clicked.
However, because I'm using 1.9.0, the preceding code produces a deprecated warning for {{#each}}
, telling me to use {{#each foo in bar}}
instead. I understand why this appears and agree the verbosity helps show exactly what data is being looped through.
So I change the line {{#each model}}
to {{#each post in model}}
and every bit of data disappears... I then try to change the code to:
updated index.html
{{#each post in model}}
<tr><td>
{{#link-to 'post' this}}
{{post.title}} <small class='muted'>by {{post.author.name}}</small>
{{/link-to}}
</td></tr>
{{/each}}
Great! The title and author's name once again appear for each post. But clicking either post gives me an undefined id
. I change {{#link-to 'post' this}}
to {{#link-to 'post' this.id}}
. Same result. I change it to {{#link-to 'post' post.id}}
. The id
is now included but when I click the link I get this error:
Error while processing route: post Assertion Failed: You used the dynamic segment
post_id in your route post, but App.Post did not exist and you did not override
your route's `model` hook.
My questions are:
What happens internally that forces the post.
prefix if I simply include the post in
code? To me I should be able to use either this
or continue to not need any prefix.
After adding post in
to the each statement, what happens to this
? Why does it no longer refer to the same object?
How can models be named to make it easier to categorize? post in model
should really be post in posts
but I haven't found a way to name the data container.
What is causing the error now that I'm no longer referring to the model as this
? How can it be remedied?
Your frustration and questions are exactly the reason why the first syntax is deprecated and only the each post in ...
form will be supported. Hopefully this answers your questions, and please respond if you need clarification.
In your first example where you use each model
, the scope of the block changes to a post, meaning this
refers to the current post in the loop. When you the form each post in ...
, the scope does not change. When it does not change, that means this
is actually referring to the previous scope (prior to the loop). In your example, the previous scope is the array controller, not the post object.
This is related to question 1. With the each post in ...
format, this
refers to whatever it was outside of the each
block. It's not that something happens to this
, it's that something does not happen to this
because the scope doesn't change.
For better naming I usually setup a property as an alias to the content in the array controller:
posts: Ember.computed.alias('content')
In your original example, when you supply the link-to
helper with this
, you're passing the full post object. From what you've tried, it looks like this is the one thing you didn't do:
{#link-to 'post' post}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With