jquery $.when error when using version 1.8.3
Question
I am keeping a variable number of ajax promises in an array. And when all promises are done I do something else:
$.when.apply($, promises).then( function(){
var myArgs = [];
if(promises.length == 1){
var myArgs = [];
myArgs[0] = arguments;
}else{
myArgs = arguments;
}
for(i=0; i<myArgs.length; i++){
fileArr[promise2File[i]]['prop1'] = myArgs[i][0].type;
fileArr[promise2File[i]]['prop2'] = myArgs[i][0].message;
}
});
When I am using jquery version 1.6.1 it works perfectly. However when I switch to a more recent version (1.8.3) I get the following js error when there are more than one ajax promise. It works fine with a single promise:
Uncaught TypeError: Object [object Object] has no method 'progress'
I checked the type of the promisses variable to make sure it is array (with instanceof Array) and it is.
In case it is helpful this is how I fill the promises array:
var i = 0;
for(var file in fileArr){
if (fileArr.hasOwnProperty(file)){
if(fileArr[file].hasOwnProperty('status')){
if($.inArray(fileArr[file]['status'], ['held','submitted']) !== -1){
promise2File[i] = file; // I need this to map each promise to the corresponding file
promises[i] = parent.myFunc('cancel', [file], false); //myFunc returns the ajax promise
i++;
}
}
}
}
Any thoughts why I am getting this error on the newer version of jquery?
Solution
in jQuery 1.8, that version of .then was removed and replaced with one that instead of accepting done/fail callbacks, accepts done/fail filters.
http://api.jquery.com/deferred.then/
Changing from .then to .done should solve your problem.
$.when.apply($, promises).done(function(){
.then
after 1.8 is essentially the same as .pipe
prior to 1.8