Save multiple models at once, in bulk

I know this question has already been discussed around stackoverflow and other forums, but I cannot find the right way to do this.

I have a model called "post" that contains information about the post (user, description, etc.). The user receives several messages, reads them and takes action on them. Messages come from my api when I request GET /api/posts/

I also have a model called post-state, where I save the action of people reading the message: when they like, share, vote or love the message.

 export default DS.Model.extend({ post: belongsTo('post'), user: belongsTo('user'), liked: attr('boolean', { defaultValue: false }), favourited: attr('boolean', { defaultValue: false }), voted: attr('boolean', { defaultValue: false }), shared: attr('boolean', { defaultValue: false }), }); 

I am looking for a way for my ember application to save my models after a large state, just as I received a large amount of mail. The message state will be saved when calling api POST /api/post-states/

I read in the discussion that the best way would be to create a custom adapter, but I'm not sure what to add to it to do this ... perhaps to create a saveAllRecords() function?

What will be the best solution for me?

Thanks for the help!

+7
javascript ember-data
source share
2 answers

You can see how ember-api-actions does this. Check the build-url and one of the files that contains the method for calling ajax. You could basically copy the methods and use them in your model. But I think it would be easier to just use the addon at this stage :)

+1
source share

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

+1
source share

All Articles