Emberjs and ember-data -creating record via form return returns undefined

I have this jsfiddle . Everything works, except that I cannot create a new comment by submitting a comment form. when i submit the form, the console shows undefined . I hope that in order to achieve this, you need to pick up the existing post and then create a comment related to this post. Thus, the flow of events, the user clicks on the message , then select a specific header to display it, and then click add comment to comment on this article. It is important to note that for now, clicking the add comment button will return undefined .

The corresponding section of code using 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()); } }); 

** Corresponding section from the handle 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> 

The comment form is submitted via 'posts / show template' using render

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

You need to create a Comment entry using:

 var comment = EmBlog.Comment.createRecord() 

or

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

You can create a record before the user fills out the form, and bind the values ​​to this created record and fix only when the user clicks the Save button, or you can bind the text area to the property of the controller, and also create and fix the record after user clicks on save.

Here's an updated script with a second approach.

+4
source

Great post. Just one comment. In latests libraries, deletePost function does not work ...

Uncaught TypeError: Unable to call 'deleteRecord' method from undefined

DECISION:

 destroyPost: function (post) { post.one('didDelete', this, function () { this.transitionTo('posts.index'); }); post.deleteRecord(); post.get('transaction').commit(); } 
+1
source

All Articles