florent-blanvillain
florent-blanvillain

Reputation: 361

hasMany relation serialize:'ids' with EmbeddedRecordsMixin, how to "sidepush"?

I am using DS.ActiveModelAdapter.

Saving a new Ember Data record to my backend, I would like to send :

{
    author: {
        name: 'Mike',
        book_ids: [null, null]
    },
    books: [
        {
            name: 'book1'
        },
        {
            name: 'book2'
        }
    ]
}

I thought it was what serialize: 'ids' was for, but when I use this configuration:

App.AuthorSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
   attrs: {
      books: {serialize: 'ids'}
   }
});

I just get :

{
    author: {
        name: 'Mike',
        book_ids: [null, null]
    }
}

Update :

The doc says : "There is an option of not embedding JSON in the serialized payload by using serialize: 'ids'. If you do not want the relationship sent at all, you can use serialize: 'no'."

So it is not clear if only the ids are added or the ids and the records aside. I would like to have confirmation here that the records are not meant to be added aside by using serialize: 'ids', and in this case what can I do to "sidepush" nested records.

Upvotes: 2

Views: 328

Answers (1)

florent-blanvillain
florent-blanvillain

Reputation: 361

It seems like EmbeddedRecordsMixin was not the way to go..

Ember data Model Fragments was the perfect solution for me. https://github.com/lytics/ember-data.model-fragments

Following the example I gave in my question, using the latter, the corresponding models would be :

App.Author = DS.Model.extend({
  name: DS.attr('string'),
  books: DS.hasManyFragments('book')
});

App.Book = DS.ModelFragment.extend({
  name: DS.attr('string')
});

Upvotes: 1

Related Questions