Can I run multiple models using Ember data?

So, let's say you have a User model that is configured for a fairly standard API. At the front end, you have an Ember project that also has this user model. A typical create call would look something like this:

store.createRecord('user', {email: ' test@gmail.com '}).save(); 

This will send a POST request to something like /api/users . However, quite extensive API support is the creation of several models at the same time. For example, instead of calling POST, just sending one object to user: {email: ' test@gmail.com '} , it sends an array of objects such as users: [{email: ' test@gmail.com '}, {email: ' test2@gmail.com '}, ...] .

As I have seen that this is handled in ember, it is easy to make several create calls at runtime. However, this is terribly inefficient, and I wonder if Ember supports saving multiple models at the same time? How do you achieve this in Amber?

+6
source share
2 answers

You cannot save an array of models in a single POST Ember Data request as you describe it, however there is a way.

You can save a parent model that hasMany 'user' with EmbeddedRecordsMixin , which will include either relationship identifiers or full records. Your serializer will look like this:

 import DS from 'ember-data'; export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, { attrs: { users: { embedded: 'always' }, } }); 

Depending on your use case, it might make sense to create a parent model just for this purpose, which hasMany 'user' . If you want to use an existing model and donโ€™t always want to insert your custom posts, there is an answer here.

If you decide to save the models separately, you would like to do users.invoke('save') , which will cause POST for each model.

+2
source

If you specifically ask about Ember Data, I donโ€™t know how to do it (I donโ€™t think that you can use any equivalent of save () for the collection / array). There may be alternative data libraries that may work (for example, you can check Orbit.JS - this is what I haven't done yet)

As I did this, it has an endpoint on my backend that receives a specific JSON payload and creates resources. You do this by calling a regular ajax call, see this example (from my project).

 let content = //get content that you want to post let accessToken = this.get('session.session.authenticated.token'); Ember.$.ajax({ data: JSON.stringify(content), dataType: 'json', method: 'POST', url: 'path/to/my/custom/end/point', headers: { 'Content-Type': 'application/json', 'Authorization': `Beader ${accessToken}` } }).then((result) => { // Code for success }, (jqXHR) => { // Code for error }).always(() => { // Code for always/finally }); 

As you can see, this is all user code, not the use of Ember Data storage or models. So far I have not found a better answer.

EDIT: After viewing the answer andorov. I forgot to say something. I am using Ember Data 2.0 (default is JSONAPI) and the EmbeddedRecordsMixin Property does not work with the JSON API

0
source

All Articles