We are using ember.data 1.13.7 and we are using the JSONAPI adapter and serializer. We are trying to submit (post) a model with all its relationships included. So far we have not been able to make this work as it is not posting the relationships and wondering what we are missing.
Model
export default DS.Model.extend({
reference: DS.attr('string'),
description: DS.attr('string'),
searches: DS.hasMany('search'),
firstSearch : Ember.computed.alias('searches.firstObject')
});
export default DS.Model.extend({
searchNumber : DS.attr('string'),
criteriaRegistrationNumber : DS.attr('string'),
dateCreated : DS.attr('date'),
searchHeader : DS.belongsTo('search-header', {inverse : 'searches'}) });
Code the create new model,
var newSearch = this.store.createRecord('search',
{
criteriaRegistrationNumber : '1234565789'
});
var newRecord = this.store.createRecord('search-header',
{
reference: 'this is a test reference',
searches : [newSearch]
});
newSearch.save();
The resulting JSONAPI does not have the expected relationship element, and include element. I have check with ember inspector that the relationship is setup correctly. e.g.
{
"data": {
"attributes": { "reference": "this is a test reference" },
"type": "searchHeaders"
}
}
We would like to find out if it is possible to instruct ember data to include the relationship in serialization.
We have also tried to use the DS.EmbeddedRecordsMixin with
export default DS.JSONAPISerializer.extend(DS.EmbeddedRecordsMixin,{
attrs: {
searches: { embedded: 'always' } // this applies to searchHeader.searches
} }
this worked to force the serialization of searches, but this produce invalid JSONAPI payload, the searches are not serialized to the include section and relationship section in search-header model is still missing.
{
"data": {
"attributes": { "reference": "this is a test reference" },
"searches": [
{
"data": {
"attributes": { "criteriaRegistrationNumber": "1234565789" },
"relationships": {
"search-header": {
"data": {
"type": "searchHeaders",
"id": null
}
}
},
"type": "searches"
}
}
],
"type": "searchHeaders"
}
}
Thanks in advance