Ember js - Hasmany relationships breaks after updating other tables Ember js - Hasmany relationships breaks after updating other tables javascript javascript

Ember js - Hasmany relationships breaks after updating other tables


When using ApplicationSerializer which extends LSSerializer, it seems to work.

Maybe it got fixed since asked?


I've noticed a few things in my path with Ember... and especially Ember-Data.

One of them is when dealing with associations I've had to manually re-add in the associations saving and having to re-save, and use addObject to in-memory associations as you're using a bit here. :)

Note that this usually only happens when I'm updating more than one new object at once. For example, if your post is new, and your comment is also new.

I'm a little worried to see the following code in your codebase, because it shouldn't need to be there. You shouldn't ever have null or non-array objects in your associations. I'm not sure what hackery you did with the Adapter and why it was necessary, but I hope that wasn't the reason:

  if(comments.get('content') == null)    comments.set('content', []);

Anyway, the following code is how I would probably write your create action. It might help. I hope it does.

create: function() {  // get the post for association on the new comment  var post = this.get('post');  // get the message to store on the new comment  var message = this.get('newMessage');  var comment = this.store.createRecord('comment', {    message : message,    post : post  });  comment.save().then(function(savedComment) {    post.get('comments').addObject(savedComment);  });}

Note that it's a lot simpler. Generally if you're doing tricky complicated things, something's amiss and it's time to go back to basics and add one thing at a time, testing thoroughly between additions. :)

Good luck!