1

在视图中保存模型后,视图中的数据为空。例如,如果我在帖子中添加评论并且在保存帖子后我的视图为空,则不会显示任何数据。为什么?我如何解决问题?

 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();
});
4

3 回答 3

1

按照转换指南,您应该将代码更改为:

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();
});

更多信息可以在这里找到。

希望能帮助到你。

于 2013-09-12T05:46:49.563 回答
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();
                });
            }
        }
    }
});
于 2013-09-12T08:56:01.430 回答
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();
                    });
                });
            }
        }
    }
});
于 2013-09-13T09:18:54.817 回答