We have spent three days trying to upgrade an app from Ember-Data pre 1.0, to 1.0. With pre 1.0 we had to use all sorts of hacks to make our client talk with the API. We were hoping it would be fine with the new version… and it’s not. At least not for us. Simple cases work fine, but as soon as things get a bit more complex, we hit serious issues.
So is it just us, or do you guys also have to use a lot of specific tweaks to make your client talk back to your API “properly”?
To be clear: I am only talking about the Ember to API direction. Loading from the API into Ember is rock solid now.
Super simple scenario (in Rails):
class Post < ActiveRecord::Base has_many :comments has_many :users accepts_nested_attributes_for :users accepts_nested_attributes_for :comments end class Comment < ActiveRecord::Base belongs_to :post validates_presence_of :post # i.e. a Comment can't exist without its Post end class User < ActiveRecord::Base has_many :posts end
Solution 1: embed records
- the simpler cases work fine: new post with new user. post.save() works well, one call to the API.
- but quickly, you face issues: new post, existing user. post.save() fails (at least in Rails). Indeed, you can’t embed an existing model into the payload of a new parent. You must pass the child by ID.
So embedded records is a no go.
Solution 2: embed IDs
New post with new comment.
How do you save the post?
- save the childs (i.e. comment) first. But in our scenario, you can’t save a comment if it’s not associated to a post. So it won’t work.
- so you must save the post first. But you will not embed any comment IDs (they don’t exist). So when the API returns, Ember thinks this post does not have any comments any more…