【问题标题】:emberjs unable to save or create a new record with emberjs- 1.0.0-rc.1 and ember-data revision-11emberjs 无法使用 emberjs-1.0.0-rc.1 和 ember-data revision-11 保存或创建新记录
【发布时间】:2013-03-09 23:27:26
【问题描述】:

我正在尝试使用 emberjs 创建一个简单的博客应用程序。如 jsfiddle 所示,为帖子创建标题和正文的功能运行良好。

现在我想添加下一个功能,即 cmets,当您单击单个帖子时应显示该功能。在首页,点击post,然后点击个人的标题,应该会出现一个评论框来添加cmets。

点击保存按钮时出现以下错误

在我使用 ember-data 和其余适配器的本地开发环境中,我收到错误:

uncaught TypeError: Cannot call method 'commit' of undefined

在JSfiddle中使用fixture适配器和同样的代码,错误变成了

TypeError: this.transaction is undefined this.transaction.commit();

posts/show.handlebars

    <script type="text/x-handlebars" data-template-name="posts/show">
      <h1>Post</h1>
      <p>Your content here.</p>
       <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/>
     <p> Add Comments</p>
      {{render 'comment/new' comments}}
   </script>

comment/new.handlebars

 <form {{action save on='submit'}}>
   {{view Ember.TextArea valueBinding='body' placeholder='body'}}
   <button type="submit"> Add comment </button>
 </form>

  EmBlog.CommentNewController = Em.ObjectController.extend({
     needs: ['posts'],
     addComment: function(){
       post = this.get('controllers.postsShow').get('model');
       comment = post.get('comments')
       this.transaction = comment.get('store').transaction.createRecord({body: body});
     },

     save: function(){
       this.transaction.commit();
     }  
   });

 EmBlog.Comment = DS.Model.extend({
   body: DS.attr('string'),
   post_id: DS.attr('integer')'
   post:  DS.belongsTo('EmBlog.Post')
 });

关于如何解决此问题的任何建议,每次我创建评论时都会包含 post_id。

 **Update**

这是latest gist,当我保存评论时它没有显示任何错误但页面上没有出现cmets。似乎它们被默默地忽略了。

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    DS.Store#transaction 是一个函数,而不是一个属性。它返回一个DS.Transaction... 但是,您会立即在其上调用createRecord,因此您实际上是将该调用的结果(一条记录)存储在您的事务属性中。此外,DS.Transaction 上的 createRecord 需要类型作为第一个参数。

    因为这是一个 ObjectController,我们必须在类定义中定义我们的内部属性,否则它们会传递给内容。

    EmBlog.CommentNewController = Em.ObjectController.extend({
        transaction: null,
    

    然后在你的实际代码中:

    var transaction = post.get('store').transaction();
    if (transaction) {
        this.set('transaction', transaction);
        transaction.createRecord(EmBlog.Comment, {body: body});
    } else { console.log('store.transaction() returned null'); }
    

    然后:

    this.get('transaction').commit();
    

    请注意,评论不会自动限定为Post,因此请务必设置您的关系。

    【讨论】:

    • 非常感谢@christopher 的帮助。我使用上面的代码 sn-p 替换了我的 jsfiddle 的相关部分,但它仍然返回一个新错误 TypeError: this.transaction is undefined。感谢您的任何新建议
    • 因为您使用的是 objectController,控制器将尝试在其内容上设置该属性。您需要在定义时将“事务”定义为控制器上的属性。
    • 谢谢@christopher。我将“事务”定义为控制器上的属性并出现此错误。当我在控制器中将其定义为 transaction: ' ' 时,它返回错误:TypeError: this.transaction.commit is not a function。但是定义为transaction: null时,返回错误:TypeError: this.transaction is null
    • 我更新了我的答案以解决一个小问题并添加一个小测试以确保现实符合预期。
    • @感谢老师耐心等待。我已经用你的代码更新了小提琴,这里是新链接:jsfiddle.net/CD9ck/1。当我尝试创建新评论时,它返回错误:TypeError: this.get(...) is null。将控制器变成数组控制器会有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多