Last active
August 29, 2015 14:03
-
-
Save Danthar/b64bd263fa27ca962f06 to your computer and use it in GitHub Desktop.
Merge data from a list of ajax requests using jQuery deferred
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dataArguments = ['args1','arg2','arg3','arg4']; | |
var bucket =[]; | |
$.when.apply($,$.map(dataArguments, function(arg){ | |
//here you setup and return an ajax request | |
//for example | |
var ajaxData = {term:arg, somethingelse:0}; | |
return $.ajax({ | |
type:"get", | |
data: ajaxData, | |
url: "something that comes from somewhere", | |
success : function(serverData) { | |
//do some processing on your serverData if need be | |
//then push the results in the bucket | |
bucket.push(serverData); | |
} | |
}); | |
})).then(function(data) { | |
//this is executed when all ajax calls have been completed | |
//we dont really care for the data var here. Since we already processed our serverData and put it into the bucket | |
console.log(bucket); //<-- contains your processed data | |
//if your not doing a collection via the bucket variable | |
//then the arguments var will contain all the arguments for this function, where each argument is basically an argument list | |
//for the success handler | |
arguments //<-- iterate through this var to process all your results | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment