Ember Data 1.13.7 using JSONAPI - how to post hasMany relationships in one payload?

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

//search-header.js

export default DS.Model.extend({
     reference: DS.attr('string'),
     description: DS.attr('string'),
     searches: DS.hasMany('search'),
     firstSearch : Ember.computed.alias('searches.firstObject')
});

//search.js

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

This is part of the JSON API 1.1 milestone: JSON API Weekly Meeting - June 22nd, 2015 - Meeting Minutes - JSON API

@jasonmit thanks - just to be clear: Loading is working for us today, it is just post/submit that is not working for us today.

To be clear for my understanding:

  1. Are you saying that the next version of Ember Data will support JSON API 1.1, which in turn supports this relationship serialization on post?

  2. Are there any workarounds in the interim?

I would also like to know a workaround for the moment. I assume we would have to extend the JSONAPISerializer in some way. However, I am not sure how to do it correctly.

@novascape do you find solution? i spend much time this

@bayarja unfortunately not and we gave up on Ember Data in the end and went with this solution that works almost out of the box with a .NET WebAPI at the backend: GitHub - toranb/ember-cli-simple-store: ember-cli addon that provides a simple identity map for ember.js web applications

Sometimes its helpful to look into the code (was pulling my hair because of this issue as well, could not believe its not possible…) - so if u still interested:

I found this https://github.com/emberjs/data/blob/v2.2.1/packages/ember-data/lib/serializers/json-api-serializer.js#L442 So,this._shouldSerializeHasMany(snapshot, key, relationship) is a method from the json-serializer and resolves in the end to this:

_mustSerialize: function (key) {
    var attrs = get(this, 'attrs');

    return attrs && attrs[key] && attrs[key].serialize === true;
  },

There you can see: just define in your serializer for your model in the attrs the relationship key like this:

attrs: { relationshipkey: { serialize : true } }

and boom - if you save your model, also the hasMany relation will be submitted. Would have been nice to find something like this in the docu… sigh

(yes, I found the place where they write about the attrs - stuff but:

You can also remove attributes by setting the serialize key to false in your mapping object. http://emberjs.com/api/data/classes/DS.JSONSerializer.html#property_attrs

does not really indicate that you explicitly have to set this to true and than and only than this one gets serialized.)