View the Project on GitHub jsonapi-suite/ember-data-extensions
This is a collection of additions/alterations to ember-data to A) add convenience helpers and B) provide functionality not part of the default addon.
You can now save full nested relation objects, specifying the relations during save()
:
post.save({
adapterOptions: {
relationships: ['tags', { author: 'state' }]
}
});
The above would send the following JSONAPI payload to the server:
{
data: {
type: 'posts',
relationships: {
tags: {
data: [
{ type: 'tags', attributes: { name: 'important' } },
{ type: 'tags', id: 123 }
]
},
author: {
data: {
type: 'authors',
attributes: { name: 'Joe Author' },
relationships: {
state: {
data: {
type: 'states',
id: 456
}
}
}
}
}
}
}
}
You can disassociate an associated object by using markForDeletion
. This will send a _delete
attribute to the server:
post.get('tags.firstObject').markForDestruction();
post.save({ relationships: 'tags' });
Sends:
{
data: {
type: 'posts',
relationships: {
tags: {
data: {
type: 'tags',
id: '123',
attributes: { _delete: true }
}
}
}
}
}
If you want to destroy the associated record as well, use markForDestruction
instead. This will send _destroy: true
instead of _delete: true
.
If an object is not dirty, we will only send its resource identifier (id
/type
) to the server (not the ember-data behavior). This is to solve a concurrency issue. Consider two users load the same post
form, where user A edits the title and user B edits the description. User A submits the form first, then user B submits. User B would overwrite the title user A entered, even though they didn't change anything.
Since we only send dirty attributes to the server, this issue is mitigated as much as possible.