【发布时间】: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