I looked around and it seems that Amber does not have the right way to do this, but I came up with something that I'm not sure what part of the Carbon Path is. What you can do is create another model that contains the hasMany attribute, which will contain the models you want to mass save, and then add these models to the container model, and you can play with the serializer / adapter to get something what you want, here is how it works:
Model (lets call it post-state-container )
import DS from 'ember-data'; export default DS.Model.extend({ postStates: DS.hasMany('post-state') });
Serializer
import DS from 'ember-data'; import Ember from 'ember'; export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, { attrs: { postStates: { embedded: 'always' }, }, serializeIntoHash: function(data, type, record, options) { Ember.$.extend(data, this.serialize(record, options)); } });
You can massage the payload sent to the server here to match what you need for your backend, because you will get a list of serialized post-state objects in JSON format from this.serialize(record, options)
Adapter
import DS from 'ember-data'; export default DS.RESTAdapter.extend({ namespace: 'api', urlForCreateRecord: function() { return this.get('namespace') + '/post-states'; }, });
How to use it (possibly, an action in a route or controller somewhere)
let record1 = this.store.createRecord('post-state'); //These would be your populated records let record2 = this.store.createRecord('post-state'); //These would be your populated records let postStateContainer = this.store.createRecord('post-state-container'); postStateContainer.get('post-state-container').pushObject(record1); postStateContainer.get('post-state-container').pushObject(record2); postStateContainer.save();
I tested this and it works. I'm not sure if there is a better way to use JSONApi or something like that
tabiodun
source share