I have a function which does an async call & calls an anonymous function on success.The data param of anonymous function is used to collect the response from the server but it is not available inside the inner function unless passed as a param.
callService('POST', getDataInfo, detailData, function (data) {
formDisplayGrid('.accruedGrid', '.accruedTable', 'Total Accrued');//doesn't work.
formDisplayGrid(data,'.accruedGrid', '.accruedTable', 'Total Accrued'); //works
});
callService is just a function which uses jQuery ajax to place a call.
function callService(method, url, data, success) {
var ajaxObject = {
url: url,
method: method,
dataType: 'json',
success: success
}
if (method == 'POST') {
ajaxObject['data'] = data;
}
jQuery.ajax(ajaxObject);
}
formDisplayGrid function just iterates over dataset to form HTML table.
function formDisplayGrid(data, modalSelector, mainGridSelector, totalLabel) {
jQuery(modalSelector).modal();
if (typeof data != 'undefined' && data.Code === 200) {
var mainGrid = jQuery(mainGridSelector);
var tbody = '';
jQuery.each(data['Data']['category'], function (k, v) {
//some code here.
jQuery.each(v['subcat'], function (k, v) {
//some code here.
});
});
//some code here.
mainGrid.find('tbody').html(tbody).fadeIn(1200);
}
}
Is that this is happening because the anonymous function is being executed in
the jQuery.ajax function and it should have been available if the anonymous function was directly executed inside the callService function?
The concept is basically simple, your formDisplayGrid() function is an external function to callback function so it shouldn't have access to the private variables within that callback function.But if you want the first approach to work u will have to define the function inside the callback function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With