emberjs和ember-data -creating记录通过表单提交返回未定义 [英] emberjs and ember-data -creating record via form submission returns undefined

查看:103
本文介绍了emberjs和ember-data -creating记录通过表单提交返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 jsfiddle 。一切正常,除了我不能通过提交评论的形式创建一个新的评论。当我提交表单时,控制台显示未定义的。我希望获得的是抓住现有的帖子,然后创建一个属于该帖子的评论。所以这个事情的流程,用户将点击发布,然后点击一个特定的帖子标题来显示它,然后点击添加评论对该帖子发表评论。请注意,现在点击添加注释按钮将返回未定义的

I have this jsfiddle. Everything works excepting that i can't create a new comment by submitting the comment's form. when i submit a form the console shows undefined. What I hope to achive is to grab an existing post and then create a comment that belongs to that post. So this the flow of things, a user will click on post then click a specific post title to display it and then click on add comment to comment on that post. It is important to note that at the moment, clicking on add comment button will return undefined.

具有 addComment 保存方法的代码的相关部分。

Relevant section of the code with the addComment and save methods.

EmBlog.CommentNewController = Em.ObjectController.extend({

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

  addComment: function(body){

    post = this.get('controllers.postsShow.model');

    store = post.get('transaction');

    store.createRecord(EmBlog.Comment, {body: body});

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

 save: function(){
  console.log(this.store.commit());
 }  
});

**手柄模板中的相关部分

**The relevant section from the handlebars template

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

评论表单通过'posts / show template'使用render

 <script type="text/x-handlebars" data-template-name="posts/show">
    <p> Comments</p>
    {{render 'comment/new' comments}}
</script>


推荐答案

您需要创建评论记录使用:

var comment = EmBlog.Comment.createRecord()

var transaction = this.get('store').transaction();
var comment = transaction.createRecord(EmBlog.Comment);

您可以在用户填写表单之前创建记录,并将值绑定到创建的记录并且只有当用户单击保存时才提交,或者您可以将文本区域绑定到控制器属性,并在用户单击保存后创建并提交记录。

You can either create the record before the user fills the form, and bind the values to that created record and only commit when the user clicks save, or you can bind the text area to a controller property, and create and commit the record after the user clicks on save.

这里是第二种方法是更新小提琴

这篇关于emberjs和ember-data -creating记录通过表单提交返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆