私は複数の延期されたAjax呼び出しを処理しており、それらのデータへのアクセス方法について動的になりたいと思っています。いくつかのパラメーターを.thenコールバックにハードコーディングしてそれぞれを個別に使用するのではなく、argumentsオブジェクトをループしてデータにアクセスしたいと思います。これは正常に機能しますが、jsonからどのデータがどのAjax呼び出しからのものであるかを判別できない場合を除きます。これは、promiseオブジェクトから(何らかの方法で)URLを決定するか、Ajax呼び出しが実行された順序を把握し、データが同じ順序であると想定することで解決できます。
これまでの私のコードは次のとおりです(私がやろうとしていることを説明するためにモックアップされています):
promises = [
$.get("example.php", {}, function(){}),
$.get("list.php", {}, function(){}),
$.get("test.php", {}, function(){}),
]
$.when.apply(null, promises).then( function() {
jsonarray = []
$.each(arguments, function(index, value){
// this is what I would like to work but doesn't
// promises[index].success.url is how I imagine accessing
//"list.php" or "test.php"
if (jsonarray[promises[index].success.url] == null){
jsonarray[promises[index].success.url] = []
}
jsonarray[promises[index].success.url] = value
doSomethingWith(jsonarray)
})
各引数をそれを生成したAjax呼び出しと一致させる別の方法はありますか?私がしたくないのはこれです:
$.when.apply(null, promises).then( function(examplejson, listjson, testjson) {
// this is lame
exampledoSomethingWith(examplejson)
listdoSomethingWith(listjson)
testdoSomethingWith(testjson)
})
ありがとう!サラ