I am using the following ajax promise for my application to load states and then districts related to that .
My question is can i call the getDistricts() function after completing loadStates done call ,rather than duplicating the code
$(document).ready(function() {
var loadStates = $.ajax({
type: 'GET',
url: url + '/depo/depo/getstates',
success: function(response) {
},
error: function(x, t, m) {
}
}); // End of loading states
loadStates.done(function() {
$("#districtsList").html("");
$.ajax({
type: 'GET',
url: url + '/depo/depo/getdistricts?stateID=' + stateIDselected,
jsonpCallback: 'jsonCallback',
success: function(response) {
},
error: function(x, t, m) {
}
}).done(function() {
$("#talukasList").html("");
var talukhtml = '<option value="">All Mandals</option>';
$("#talukasList").html(talukhtml);
$(".gobtn").trigger("click");
});
})
$("#hideorshow").hide();
}); // ready call ends here
function getDistricts() {
$.ajax({
type: 'GET',
url: url + '/depo/depo/getdistricts?stateID=' + stateIDselected,
jsonpCallback: 'jsonCallback',
dataType: 'jsonp',
success: function(response) {
},
error: function(x, t, m) {
}
});
}
please let me know how to achieve this ??
Get any Ajax functions to return their own promise:
function getDistricts() {
return $.ajax({
type: 'GET',
url: url + '/depo/depo/getdistricts?stateID=' + stateIDselected,
jsonpCallback: 'jsonCallback',
dataType: 'jsonp'
});
}
Make all your Ajax calls into similar functions (same is good):
function loadStates(){
return $.ajax({
type: 'GET',
url: url + '/depo/depo/getstates'
});
}
Then you can just chain the operations.
$(document).ready(function() {
loadStates().done(function() {
$("#districtsList").html("");
getDistricts().done(function(){
$("#talukasList").html("");
var talukhtml = '<option value="">All Mandals</option>';
$("#talukasList").html(talukhtml);
$(".gobtn").trigger("click");
});
})
$("#hideorshow").hide();
});
done()
callbacks so just add the parameters there.Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments