Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 模型、命名和此_Javascript_Ember.js_Handlebars.js - Fatal编程技术网

Javascript 模型、命名和此

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

在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.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.