Ember to back-end: are there recommended conventions?


#1

We are transitioning from Ember-Data pre 1.0 to 1.0. The guides are very clear regarding how to format JSON on the back-end when sending it to Ember. The fact Ember will only accept records with IDs also helps a lot to structure things.

But we are struggling with the other side of the problem: Ember to back-end. I understand it’s very much linked to the back-end used, but I was hoping someone could point me to a good (“definitive”?) guide covering the conventions used when interfacing with Rails + AMS.

Typical problem: model A has_many B.

  • case 1: a new A is created in the client, and attached to a new B. We embed B into A’s payload and everything is fine.
  • case 2: a new A is created in the client, and attached to an existing B. If we embed B into A’s payload, the server will not accept the payload (for security reasons). So it means in this case, we should only embed B’s id, not the whole record.
  • case 3: a new A is created and linked to both an existing B, and to a newly created B. I don’t know how to handle this.

With multiple levels of relationships, it can become quite messy.

Is there a systematic, recommended way to handle this?


#2

I can tell you what I am doing. I am only linking records by ID and setting the relationship to async. That way I am not returning more data than needed. I do have concern about the chattiness of this approach since requests will be made to resolve those relationships when accessed, but I will tweak and maybe return embedded data when it makes since or I have performance issues. I feel this is the way it is designed to work, but hopefully others with more ember experience will chime in.


#3

Thanks @cphill11. I think - if I understood correctly - what you describe applies more to the Back-end -> Ember side of things. We are fine with this part. We tend to side load records but I guess it’s mostly a matter of taste.

Our problem is with the Ember -> Back-end side of the communication. “Linking records by ID” is a bit tricky in this case, since it means you need to save the child first, and then only you can save the association with the parent. It is doable, but when a complex relationship graph is involved, it can become tricky I think.


#4

Do u already look The network in dev tools of the browser? We can check The payload in POST reques to know how the data are going


#5

@heatbr Yes absolutely. I definitely know what’s going on. I am not trying to solve a specific issue/example. My question really was: is there a recommended/conventional way to send parents and childs records back to the server “in general”? If you embed records, there are many instances where problems occur (at least in my experience). Cf case 3- above for instance. There is also the issue with records being duplicated (Proposal/Fix: Saving new embedded records creates duplicates), etc.

Basically I am having so many issues, I am thinking I just don’t tackle it the right way and there’s got to be another one. I just don’t know what it is. Our app is fairly complex with a lot of relationships between models so the challenge might be exacerbated, but still.