【发布时间】:2013-04-02 22:35:52
【问题描述】:
我有一个帖子,里面有很多 cmets。问题是我能够获取所有 cmets,但我无法获取单个评论,因此无法编辑单个评论。由于我无法获取单个评论,这意味着我无法将单个记录添加到事务或编辑单个记录。
cmets 是侧载的,不会由路由支持,我不想要任何与评论相关的控制器的路由。因此,我使用 controllerFor 在 ApplicationRoute 中为 CommentController 设置模型,然后使用 [needs] api 将模型包含在可能需要模型内容的其他评论相关控制器中。
您可以通过点击post -> 然后点击post title -> 联系 cmets 点击添加评论*然后保存并重新点击editcomment。
这是jsfiddle,但与此问题相关的代码如下:
EmBlog.ApplicationRoute = Ember.Route.extend({
setupController: function() {
this.controllerFor('comment').set('model', EmBlog.Comment.find());
}
});
评论控制器
//Even when I use ArrayController, the error is still there
EmBlog.CommentController = Ember.ObjectController.extend({
content: null
});
处理编辑的控制器,所有的错误都发生在editComment方法中
EmBlog.CommentEditController = Ember.ObjectController.extend({
needs: ['comment'],
isEditing: false,
editComment: function(post) {
var comment = this.get('controllers.comment.content');
var yk = comment.get('post');
//this line is undefined
console.log(yk);
var commentEdit = this.set('content', comment);
console.log(commentEdit);
transaction = this.get('store').transaction();
//Uncaught Error: assertion failed: You must pass a record into transaction.add()
transaction.add(commentEdit);
this.transaction = transaction;
this.set('isEditing', true);
}
});
用于发布/展示的把手
<script type="text/x-handlebars" data-template-name="posts/show">
{{render 'comment/edit' comment}}
</script>
【问题讨论】:
标签: ember.js ember-data