I am using the loading substate of a route to show to the user that some network traffic is happening. As described here: http://ift.tt/1p28qrk
In addition to load a model in the model hook, I also saves changes during the route initialization phase. I do that in the aftermodel hook. Something like this:
afterModel () {
var session = this.get('session');
return session.SaveAnyUnsavedObjects();
},
//session.js
SaveAnyUnsavedObjects: function() {
var promises = [];
...
activities.forEach(function(activity) {
if(activity.get('hasDirtyAttributes')) {
promises.pushObject(activity.save());
}
}
if(promises.get('length') > 0)
{
// Use RSVP.Promise.allSettled() to resolve an array of promises.
return Ember.RSVP.allSettled(promises);
}
}
This works well in most cases. The problem arises when some or all my promises fail, possibly due to offline. So I want to add a "then"-handler to my promises, so can execute some code when the promises are done. How can I add a then-handler and at the same time keep the loading substate?
In other words, how can I return a promise to the model hook or aftermodel hook as soon as the ajax call starts and at the same time have code that handles the result of the promise?
Aucun commentaire:
Enregistrer un commentaire