devour-js/devour-client

Adding to relationships

Closed this issue · 0 comments

Josta commented

I'm confused as to how the Devour API would allow to add to or remove from a relationship.
Lets say I have this model type definition:
jsonApi.define('user')
jsonApi.define('event', {title: '', leaders: {jsonApi: 'hasMany', type: 'users'}})

Now I found that the following code works, but only if the relationship is named like the model, i.e. 'users' instead of 'leaders':
jsonApi.one('events', 353).relationships().all('user').post([{type: 'users', id: 222}])
jsonApi.one('events', 353).relationships().all('user').destroy([{type: 'users', id: 222}])

Unless I missed something, I suggest to fix post/destroy so that this is possible:
jsonApi.one('events', 353).relationships().all('leaders').post([{type: 'users', id: 222}])
jsonApi.one('events', 353).relationships().all('leaders').destroy([{type: 'users', id: 222}])

Or even better, introduce new methods add and remove like this:
jsonApi.one('events', 353).add('leaders', [222])
jsonApi.one('events', 353).remove('leaders', [222])