【问题标题】:emberjs in the console I see that records are created but they are not displayed by handlebars控制台中的 emberjs 我看到记录已创建,但车把未显示它们
【发布时间】:2013-03-27 18:47:44
【问题描述】:

首先非常感谢 Mike Grassotti,感谢他在 IRC 中提供的帮助。他通过 save 方法帮助解决了错误。

我的问题是在控制台中我看到记录已创建但未显示。

我正在使用 ember-data 创建新记录。 addComment 函数在事务中创建记录,而save函数只调用this.transaction.commit .

在我单击 save() 后的控制台中,记录似乎已创建,但车把不显示新创建的记录。这是我在深入研究 console.log 的结果时在控制台中看到的摘录

 >committed: Object
    firstRecordKind: "belongsTo"
    firstRecordName: "post"
 >firstRecordReference: Object
   clientId: 4
   id: "ember411"
>type: EmBlog.Comment
   ClassMixin: Ember.Mixin
  >FIXTURES: Array[1]
    0: Object
      body: "ty"
      id: "ember411"
      post: "1"

要创建一条新记录,点击post -> 然后点击'post title' -> 在底部addComment-> 然后保存,你会看到记录没有创建。

来自jsfiddle 的相关代码。此控制器将没有路由,因为它将被侧载

EmBlog.CommentNewController = Em.ObjectController.extend({

 needs: ['postsShow'],
 isAddingNew: false,

 addComment: function(body){
    console.log("this: ", this.toString());
    var post = this.get('controllers.postsShow.content');
    console.log(post);

   transaction = this.get('store').transaction();
   console.log(transaction);
   console.log(this.get('content').toString());

   this.set('content', transaction.createRecord(EmBlog.Comment, ({post: post })));
   this.transaction = transaction;

   console.log(this.get('content').toString());

   this.set('isAddingNew', true);
 },

 save: function(){
   var comment = this.get('content');

   comment.one('didCreate', this, function() {
      this.set('isAddingNew', false);
   });

  this.transaction.commit();

 }  

});

车把模板中的相关位

<script type="text/x-handlebars" data-template-name="posts/show">
  <h1>Post</h1>

  <h3> {{title}} </h3>
  <h3> {{body}} </h3>
   <br/>
  <p> {{#linkTo 'posts.index'}} back {{/linkTo}}</p>
  <p> {{#linkTo 'posts.edit' content}} Edit the post  {{/linkTo}}</p> 

  <br/>
  <b> Comments</b>
   {{render 'comment/new' comments}}
</script>

<script type='text/x-handlebars' data-template-name='comment/new'>
     {{#if controller.isAddingNew}}
     <form {{action save on='submit'}}>
    {{view Ember.TextArea valueBinding="body" placeholder="body"}}
    <button type="submit"> save comment </button>  
    </form>
     {{/if}} 

    <br/>

   <div>
   <button {{action addComment}} {{bindAttr disabled="isAddingNew"}}>Add Comment</button>
  </div>
</script>

谢谢

【问题讨论】:

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


    【解决方案1】:

    嗯,也许我是盲人,但我看不到任何用于在您的模板中显示 cmets 的代码。

    类似

    <ul>
    {{#each comment in comments}}
      <li>{{comment.body}}</li>
    {{/each}}
    </ul>
    

    应该可以解决问题。

    【讨论】:

    • 谢谢你是对的。我跳过了那个。它现在正在显示。
    • 对于任何感兴趣的人,这是 working jsfiddle
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2015-06-30
    相关资源
    最近更新 更多