Created
August 15, 2016 20:47
-
-
Save franklin-like-the-turtle/3008115f6d08d59d71fa9d07ecc33f98 to your computer and use it in GitHub Desktop.
Handle Results of Multiple AJAX Requests with jQuery Promises
This file contains hidden or 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
function getReq(req, reqType) { | |
var fy1req = req + '/year/' + perDiemSearch.startFY; | |
function getStartFY() { | |
return $.ajax({ | |
url: fy1req, | |
}).done(function(data) { | |
//no rates | |
if (!data.rates || data.rates.length === 0) { | |
//if search has state, search again for state | |
if (reqType === 'city-state') { | |
//build a new request, ignore city | |
buildReq(true); | |
} else { | |
//else error | |
locationError() | |
} | |
reqError = true; | |
} else { | |
reqError = false; | |
var rates = data.rates[0].rate; | |
if (rates.length > 1) { | |
//multiple rates are available for FY1 | |
for (i in rates) { | |
if (rates[i].county === ' ') { | |
rates[i].county = 'Standard Rate' | |
} | |
} | |
perDiemSearch.rates.fy1 = { | |
year: perDiemSearch.startFY, | |
multiple: true, | |
rates: rates | |
} | |
} else { | |
perDiemSearch.rates.fy1 = { | |
year: perDiemSearch.startFY, | |
multiple: false, | |
rate: rates[0] | |
} | |
} | |
} | |
}) | |
.fail(function() { | |
$('#perdiem-multiple-rates-check').html('Next') | |
reqError = true; | |
$('#perdiem-api-error').show() | |
}) | |
} | |
//if search includes 2 fiscal years | |
function getEndFY() { | |
if (perDiemSearch.startFY !== perDiemSearch.endFY) { | |
var fy2req = req + '/year/' + perDiemSearch.endFY; | |
return $.ajax({ | |
url: fy2req, | |
}).done(function(data) { | |
//data = JSON.parse(data) | |
if (!data.rates || data.rates.length === 0) { | |
//if search has state, search again for state | |
if (reqType === 'city-state') { | |
//build a new request, ignore city | |
buildReq(true); | |
} else { | |
//else error | |
locationError() | |
} | |
reqError = true; | |
} else { | |
reqError = false; | |
var rates = data.rates[0].rate; | |
if (rates.length > 1) { | |
for (i in rates) { | |
if (rates[i].county === ' ') { | |
rates[i].county = 'Standard Rate' | |
} | |
} | |
perDiemSearch.rates.fy2 = { | |
year: perDiemSearch.endFY, | |
multiple: true, | |
rates: rates | |
} | |
} else { | |
perDiemSearch.rates.fy2 = { | |
year: perDiemSearch.endFY, | |
multiple: false, | |
rate: rates[0] | |
} | |
} | |
} | |
}) | |
.fail(function() { | |
$('#perdiem-multiple-rates-check').html('Next') | |
$('#perdiem-api-error').show() | |
reqError = true; | |
}) | |
} else { | |
return true | |
} | |
} | |
$.when(getStartFY(), getEndFY()).done(function() { | |
if (reqError === true) { | |
} else { | |
//if multiple rates available, show multiple rates UI | |
if (perDiemSearch.rates.fy2) { | |
if (perDiemSearch.rates.fy1.multiple || perDiemSearch.rates.fy2.multiple) { | |
displayRates() | |
} else { | |
calculateRates() | |
} | |
} else { | |
if (perDiemSearch.rates.fy1.multiple) { | |
displayRates() | |
} else { | |
calculateRates() | |
} | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment