Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Data bulk saves to server

Ember Data is moving fast from version to version and the method for saving data has been changing along with it. Right now with version 1.0.0-beta.8.2a68c63a the proper method is to update a record and then do a record.save() to trigger a PUT request back to the server. With my current app I'm updating multiple records at once and that could involve 50+ PUT ajax requests back to the server. We're concerned about performance and efficiency issues and haven't found any documentation for doing batch requests. If anything, we've found a lot of other people online that are looking to do the same thing and haven't found a good solution.

Right now I'm looking into manually serializing these objects and saving them back to the server, which I thought was the entire point of Ember Data. So maybe it's in my best interest to not use Ember Data at all and manually code out CRUD requests and make my own data layer for handling all of this and just use ArrayControllers and ObjectControllers to save the data in and bind to the Handlebars templates. It seems like the benefits to this would be that it would work the way I need it to (batch requests) and the code won't break with future versions of Ember Data. Any thoughts or solutions?

like image 244
Cory Schulz Avatar asked Nov 11 '22 06:11

Cory Schulz


1 Answers

You can do it with the DS.EmbeddedRecordsMixin. If your Page object has many Blocks (for example, based on your comment), then page-serializer.js would look like:

import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    blocks: { serialize: 'records' }
  }
});

Whenever you save a page record all of its associated blocks records will be included in that one PUT request.

See the documentation here.

like image 87
andorov Avatar answered Dec 18 '22 07:12

andorov