I've coded this in jQuery 1.7:
$.when($.ajax({
type: "GET",
url: internalOrderServiceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: $.proxy(this.retrieveInternalOrderSuccess, this),
error: $.proxy(this.retrieveInternalOrderError, this)
}), $.ajax({
type: "GET",
url: rejectionReasonServiceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: $.proxy(this.retrieveRejectionReasonSuccess, this),
error: $.proxy(this.retrieveRejectionReasonError, this)
})
).done(
$.ajax({
type: "GET",
url: salesOrderInfoServiceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: $.proxy(this.retrieveServiceItemSuccess, this),
error: $.proxy(this.retrieveServiceItemError, this)
})
);
However the callback retrieveServiceItemSuccess is executed before retrieveInternalOrderSuccess and retrieveRejectionReasonSuccess. Could anybody tell me what's wrong with that?
I've changed the code to this:
$.when($.ajax({
type : "GET",
url : internalOrderServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveInternalOrderSuccess, this),
error : $.proxy(this.retrieveInternalOrderError, this)
}), $.ajax({
type : "GET",
url : rejectionReasonServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveRejectionReasonSuccess, this),
error : $.proxy(this.retrieveRejectionReasonError, this)
})).done(function() {
$.ajax({
type : "GET",
url : salesOrderInfoServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveServiceItemSuccess, this),
error : $.proxy(this.retrieveServiceItemError, this)
})
});
but this time, the first callback retrieveInternalOrderSuccess executes then the second callback executes (retrieveRejectionReasonSuccess) - the order of execution of these two callbacks is random. However the third callback does NOT execute. Could anybody advise what's wrong?
I have tried to add this:
var self = this;
$.when($.ajax({
type : "GET",
url : internalOrderServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveInternalOrderSuccess, this),
error : $.proxy(this.retrieveInternalOrderError, this)
}), $.ajax({
type : "GET",
url : rejectionReasonServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : $.proxy(this.retrieveRejectionReasonSuccess, this),
error : $.proxy(this.retrieveRejectionReasonError, this)
})).done(function() {
$.ajax({
type : "GET",
url : salesOrderInfoServiceURL,
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(oResult) {
self.retrieveServiceItemSuccess(oResult);
},
error : function(oResult) {
self.retrieveServiceItemError(oResult);
},
})
});
and this time the callbacks are called in the right order. Can anybody clarify this?