Hi
I have a model returning from the backend using JSON API.
// models/client.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
full_name: DS.attr(),
contacts: DS.attr(),
});
One of the attributes is an array of properties, “contacts”, coming from a jsonb column in the clients table in PostgreSQL. Not from a related table.
{
"data": {
"id": "230bc966-b109-4dc8-b99d-7ac29bbbb883",
"type": "parties",
"attributes": {
"name": "Gran tienda",
"full-name": "Gran tienda del universo",
"contacts": {
"included": [{
"id": 1,
"type": "contacts",
"attributes": {
"name": "pepe",
"phone": "91 000 00 00"
}
}, {
"id": 2,
"type": "contacts",
"attributes": {
"name": "juan",
"phone": "91 000 00 11"
}
}, {
"id": 3,
"type": "contacts",
"attributes": {
"name": "fernando",
"phone": "91 000 00 22"
}
}]
}
}
}
}
I have no problem to show, edit and save the Contacts elements, but it seems that “hasDirtyAttributes” do not detect changes in the nested Contacts.
I’ve read some things about similar problems, but the problems and the solution tricks are related to the REST API, not the JSON API. It is supposed that JSON API “understand” structures like mine, so no tricks should be necessary, isn’t it?
My intuition tells me that I must create a contacts model, like in a relationship:
// models/contact
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
phone: DS.attr()
});
And find a way to relate it to the client model.
export default DS.Model.extend({
name: DS.attr(),
full_name: DS.attr(),
contacts: DS.hasMany('contact')
});
But there is not a true relationship between tables or entities, so I am really lost at this point.
I have also inserted “included”: [{ … }] as the property of the array, and the “id” and “type” attributes to maintain the JSON API data structure.
Can someone point me n the right direction to find the missing pieces?
Than you in advance:
Nacho B.