I'm wondering what would be the best design for chaining Deferreds. Please consider the following snippet:
Component X:
_functionRaisingEvent: function()
{
$.when(this.raiseEvent('myEvent', args)).done(function(args){ // Check for args.cancel });
},
_raiseEvent: function(event, args)
{
var def = new $.Deferred();
// Call subscribers of myEvent in different components and pass them args
// Subscribers possibly make ajax calls
return def.promise();
},
Subscriber Y:
_onMyEvent:function(args)
{
$.ajax(....).done(function(data){ if(data == 1) args.cancel = true;});
},
Subscriber Z:
_onMyEvent:function(args)
{
document.getElementById('abx').display = 'block';
}
_functionRaisingEvent calls _raiseEvent which fires the callbacks of the subscribers.
What I’d like to achieve is, that the callback of done in _functionRaisingEvent is only executed after all subscribers finished.
Should I pass the subscribers their own Deferred as argument and use pipe? Is the any best practice or design pattern?