pierred69 February 2016

emberJS 2 : nested relationships resources for actions (update & delete)

I'm asking you about relationships with Ember Data.

I have a User that belongs to an account and a cloud. Datas are well associated with URLs.

The API is done like this :

GET users/:id -> send the user

GET users/:id/account -> send the account status infos

GET users/:id/cloud -> send the cloud status infos

What I need :

I need to be able to update the account and cloud using the user id info. The API expects this :

PATCH users/:id -> update the user

PATCH users/:id/account -> update the account associated to the userId. The userId is enough in the API side, but we can if necessary send the account id like this -> users/:id/account/:idaccount

The same URLs with the DELETE http method to DELETE accounts.

When I trigger a "model.get('account').get('content').save() it sends

accounts/:idaccount

But our API doesn't work like this...

Do you have an idea about how I could manage it ?

Thanks a lot !

Answers


James A. Rosen February 2016

If you're using Ember-Data's JSONAPIAdapter or any other RESTAdapter, then you want to override urlForUpdateRecord. Snapshot.prototype.belongsTo will also be useful. I'd guess something like

// app/adapters/account.js:
urlForUpdateRecord(id, modelName, snapshot) {
  const userId = snapshot.belongsTo('user', { id: true });
  return `/users/${userId}/account`;
}

Post Status

Asked in February 2016
Viewed 3,344 times
Voted 9
Answered 1 times

Search




Leave an answer