【问题标题】:Emberjs-1.0.0 template not displaying recordEmberjs-1.0.0 模板不显示记录
【发布时间】:2013-12-13 19:57:06
【问题描述】:

在我的 ember 应用中,ma​​il 模板仅在传递给 App.MailRoute 的模型未使用 filterProperty 过滤时显示。这是 jsfiddle of the working version not filtered。这是jsfiddle that doesn't work after filtering with filterProperty。它使用的模板是 ma​​il.handlebars 模板,它被粘贴在显示此问题下方路线的代码下方。但是,当您注释掉 App.MailRoute 的代码中显示的 setupControllers 时,一切都会像 ma​​il.handlebars 模板 中那样显示记录。但是,当您单击其父模板(即 ma​​ils.handlebars 模板)中的第一个链接,然后立即单击父模板中的第二个链接模板,它会抛出错误Uncaught TypeError: Cannot read property 'firstChild' of null。这似乎是由模型之间的 async: hasMany 关联引起的。如果我过滤App.MailRoute的内容,当我点击第一个链接然后立即点击第二个链接时不会抛出上述错误strong> 但认为没有错误,即使控制台显示模型填充了正确的数据,模板也不会显示任何内容。

路由器

App.Router.map(function() {
  this.resource('mails', function() {
    this.resource('mail', { path: ':id' });
  });
});

App.MailsRoute

App.MailsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('mail');    
  }
});

App.MailRoute

 App.MailRoute = Em.Route.extend({
    setupController: function(controller, model) {
       console.log('model', model);

       a = this.controllerFor('mails').filterProperty('id', model.id);
       console.log('a', a);

      controller.set('model', a);
      console.log('model after setupcontroller', model);
    } 

 });

ma​​ils.handlebars 模板

<script type='text/x-handlebars' data-template-name='mails'>
  <h6> from mails template </h6>
  {{#each item in  model}}
   {{#link-to 'mail' item }} {{item.name}} ( {{item.messages.length}} ) {{/link-to}}
  <br/>
  {{/each}}
  {{outlet}}
</script>

ma​​il.handlebars 模板

<script type='text/x-handlebars' data-template-name='mail'>
 <br/>

  {{log record}}
 {{controller}}
<h6> individual mail template </h6>

  {{type}} or {{name}}

  <div>
  <table>
  <tr>
  <th>From</th>
  <th>To</th>
  <th> Subject </th>
  <th> Body </th>
  <th>Date</th>
  </tr>

 {{#each messages}}

     <td>{{from}}</td>
     <td>{{to}}</td>
    <td>{{subject}}</td>
    <td>{{body}}</td>
     <td> {{date}}</td>
     <br/>
 {{/each}}
 </table>

  {{outlet}}
</div>

App.Mail 模型

App.Mail = DS.Model.extend({
  name: DS.attr('string'),
  type: DS.attr('string'),
  messages: DS.hasMany('message', {async: true})
});

App.Message 模型

App.Message = DS.Model.extend({
  subject: DS.attr("string"),
  from: DS.attr("string"),
  to: DS.attr("string"),
  date: DS.attr('date'),
  body: DS.attr("string"),
  mail: DS.belongsTo('mail')
});

【问题讨论】:

    标签: ember.js ember-data handlebars.js ember-router


    【解决方案1】:

    真正的问题是桌子。如果您在没有表结构的情况下查看所有数据,它会起作用。将所有内容重新添加,您会注意到
    破坏了它。删除它,你应该会很好。

    此外,您应该实现模型挂钩,否则刷新将不起作用。

    {{#each messages}}
    
        <td>{{from}}</td>
        <td>{{to}}</td>
        <td>{{subject}}</td>
        <td>{{body}}</td>
        <td>{{date}}</td>
    
    {{/each}}
    

    http://jsfiddle.net/fnU9z/3/

    【讨论】:

    • 非常感谢@kingpin2k。你知道为什么
      标签会导致这个问题吗?我不需要
      标签,但我想我应该问你是否有任何解释来学习。
    • 表格在 Ember 中很棘手,额外的标签似乎会破坏视图构建器。我不确定确切的原因。
    • 呸,由于类似的原因,我遇到了同样的错误 - 在表格内部:/
    【解决方案2】:

    你的问题是你在这里分配一个数组:

    controller.set('model', a);
    

    a是一个数组,filterProperty返回一个数组,你只需要传递一个元素。

    我认为您需要使用 findByProperty,这取决于您使用的 ember 版本。 findBy 可能适合你

    【讨论】:

    • 我认为您需要使用 findByProperty,这取决于您使用的 ember 版本。 findBy 可能适合您。
    • 感谢您的建议,请注意不是我反对您。使用 findBy 有效,但它让我回到当我单击第一个链接并立即单击第二个链接时,我收到错误 Uncaught TypeError: Cannot read property 'firstChild' of null .
    • 当然,别担心,很高兴有人给了你正确的答案。
    • 大声笑,这完全是我,这是一个意外,肩部手术让我用左手,我错过了,然后试图修复它,但它说我不能改变投票除非它被编辑,所以我编辑了你的帖子然后修复它;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多