After save model data in view empty


#1

Hi,

after save model in view, the data in view are empty. For example if I add an comment to post and after saving the post my view is empty, no data will display. Why? How I solve the problem?

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

#2

Here is my controller, after save my comments would not display why?

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

#3

I solved the problem, with ‘post.reload()’ after the post would succesful updated.

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

#4

Although you’ve found a workaround, it is still ugly: you have to reload the model by issuing an extra HTTP request. I’ve created a JSBin to demo the problem: http://jsbin.com/EWUSEkA/3/edit?html,js,output

And also a stackoverflow question http://stackoverflow.com/questions/18792235/how-to-save-changes-to-ember-js-model-without-breaking-its-relationships