在视图中保存模型后,视图中的数据为空。例如,如果我添加评论发布,保存帖子后我的视图为空,则不会显示任何数据。为什么?我是如何解决这个问题的?
var post = this.get('model');
var comment = this.store.pushObject('comment');
comment.set('text', ' thats some text');
comment.set('created', '11111212');
comment.save().then(function(resolvedComment){
post.get('comments').addObject(resolvedComment);
post.save();
});
答案 0 :(得分:1)
在transition guide之后,您应该将代码更改为:
var post = this.get('model');
var comment = this.store.createRecord('comment');
comment.set('text', ' thats some text');
comment.set('created', '11111212');
comment.save().then(function(resolvedComment){
post.get('comments').pushObject(resolvedComment);
post.save();
});
可以找到更多信息here。
希望它有所帮助。
答案 1 :(得分:0)
好的,你拿到我的控制器,如果点击保存按钮,方法就会激活。
App.PostController = Ember.ObjectController.extend({
needs: ['application'],
newComment: null,
actions:{
createIncidentComment: function(){
var post = this.get('model');
var state = post.get('postState');
var newComment = this.get('newComment');
var date = Date.now();
var currentUser = this.get('controllers.application.user');
var contr = this;
if(newComment!= null){
var comment = this.store.createRecord('comment',{
comment: newComment,
created: date,
context: postState,
reporter: currentUser
});
comment.save().then(function(){
post.get('comments').pushObject(comment);
post.save();
});
}
}
}
});
答案 2 :(得分:0)
问题解决了:))
保存帖子后,我实际上重新加载了模型。在我的例子中,模型是帖子。
App.PostController = Ember.ObjectController.extend({
needs: ['application'],
newComment: null,
actions:{
createIncidentComment: function(){
var post = this.get('model');
var state = post.get('postState');
var newComment = this.get('newComment');
var date = Date.now();
var currentUser = this.get('controllers.application.user');
var contr = this;
if(newComment!= null){
var comment = this.store.createRecord('comment',{
comment: newComment,
created: date,
context: postState,
reporter: currentUser
});
comment.save().then(function(){
post.get('comments').pushObject(comment);
post.save().then(function(){
post.reload();
});
});
}
}
}
});