It works but I have problems with my submit method. First, I think it’s ugly, I don’t like nested then. Secondly, if there is a failure with a server side validation, it will not continue and trigger other validations. If some models fail, others can be created on the server side.

I did not found any clean solution on the Internet. The best way can be to pass all data in a single xhr call. I tried without success to pass nested attributes on models.

It looks like subscription and contacts are unrelated to each other? In that case, there’s no need to nest the save() other than ensuring the initial user save worked. Basically, you could end up with an array of promises where some succeeded and some failed. So It might look something like this…

Your solution looks nice but I think I will have a problem. I have to wait the subscription for have the id and pass it to the user. Again, I have to wait the user response to save contacts with the user id.

I still think the best solution is to pass all arguments in a single call but I think it’s not available for JSON api yet and others serializers seems to not be designed for that.

I can’t believe than there is no simple solution. Maybe I’m fighting against the ember way but I don’t know what’s the ember way. Do you have an idea?

I have to wait the subscription for have the id and pass it to the user.

I wonder if your Ember model doesn’t accurately represent the relationship on the server side. On the server side is it user.subscription_id = subscription.id? I assumed it was the other way around. If it’s Subscription -> hasOne (or hasMany) -> User then I think you’d need to save the subscription first?

However, more generally, the code as you have written can be made into a strict hierarchy of A must succeed, then try B, then try C… by adding the “error” callback as a second parameter to then.

subscription.get('user').then((user) => {
user.save().then(() => {
subscription.save().then(() => {
// ERROR: there's a different error in the next line, I think:
// you just got 'user' back from the server, but that saved user has NO contacts (they are all
// still just on the browser side). You'd have to do something to add the unsaved Contacts to the User
// before saving them.
RSVP.allSettled(user.get('contacts').invoke('save')).then((array) => {
if(arrayAllSucceeded(array)) {
this.transitionTo('subscriptions.success');
} else {
// handle errors in one or more contacts
}
});
}, (errs) => {
// subscription wasn't valid, handle that....
});
}, (errs) => {
// user wasn't valid, handle that....
})
})