Javascript 模型、命名和此
在EmberJS的网站上运行时,有几件事让我有点困惑 需要立即注意的一点是,我决定使用ember 1.9.0beta4和车把2.0.0,而不是1.8.1/1.3.0包含在启动包中 首先,屏幕广播中包含的代码:Javascript 模型、命名和此,javascript,ember.js,handlebars.js,Javascript,Ember.js,Handlebars.js,在EmberJS的网站上运行时,有几件事让我有点困惑 需要立即注意的一点是,我决定使用ember 1.9.0beta4和车把2.0.0,而不是1.8.1/1.3.0包含在启动包中 首先,屏幕广播中包含的代码: app.js App.Router.map(function() { this.resource('about'); this.resource('posts'); this.resource('post', {path: ':post_id'}) }); App.Post
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;
}
});
及
太好了!每个帖子的标题和作者姓名都会再次出现。但单击任一帖子都会给我一个未定义的id
。我将{{{链接到'post'this}}
更改为{{{链接到'post'this.id}
。同样的结果。我把它改为{{{#链接到'post'post.id}
。id
现在已包含在内,但当我单击链接时,会出现以下错误:
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.
我的问题是:
帖子,那么会在内部发生什么情况来强制使用帖子。
前缀?对我来说,我应该能够使用这个
或者继续不需要任何前缀
post-in
添加到每条语句后,此会发生什么变化?为什么它不再指同一个对象
post-in-model
应该是post-in-posts
,但我还没有找到命名数据容器的方法this
,是什么导致了错误?如何补救您的沮丧和疑问正是第一种语法不受欢迎的原因,而且只有…表单中的
每篇文章将受到支持。希望这能回答您的问题,如果您需要澄清,请回复
在您使用每个模型的第一个示例中,块的范围更改为一个post,这意味着此
引用循环中的当前post。当您在…
中输入表单时,范围不会更改。当它没有改变时,这意味着这个实际上是指上一个作用域(在循环之前)。在您的示例中,前面的作用域是数组控制器,而不是post对象
这与问题1有关。当每篇文章都是…
格式时,这篇引用了每篇块之外的内容。这并不是因为这个
发生了什么,而是因为这个
没有发生什么,因为范围没有改变
为了更好地命名,我通常将属性设置为数组控制器中内容的别名:
posts:Ember.computed.alias('content')
在原始示例中,当您使用this
提供指向
帮助程序的链接时,您传递的是完整的post对象。从您的尝试来看,这似乎是您没有做的一件事:
{#链接到'post'post}
我将尝试按顺序回答您的问题:
当你说{{{#each model}}
时,你是在模型中的post(数组)中循环,因此每次循环时,这个
都是指当前post。因此,当你说{{title}}
时,你实际上是在说{{this.title}}
当你说{{{{模型中的每个post}}}
更明确时,那么循环中的每个迭代不再引用这个
,而是引用你调用的变量post
就像上面#1中提到的,这个
不再指每个单独的迭代。我理解您的想法,也许仍然能够使用此
(与post
)会很方便,但请考虑以下场景。当您有一个嵌套的{{{each}
时会发生什么?隐式this
指的是外部数组还是内部数组?如果您真的不想键入额外的post.
您可以始终使用{{{{with post}}
把手帮助程序,该帮助程序将post
返回到此
请参见以下示例
如果您的模型或控制器中有一个属性,您完全可以像在{{{#each color in colors}}}
中那样循环使用该属性,请参阅以获取工作示例
最后,链接到
的应该是{{{{链接到'post'post}}
我认为你和KalmanHazins一起完美地澄清了一切。谢谢
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}}
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.