I have a function that looks like this:
function showCreditCard(idx, data) {
if(typeof cardInfo == 'undefined' && parseInt($("#cc-dropdown-" + idx + " option:selected").val(),10) > -1) {
// actual selection made, but not default, so cardInfo hasn't been set. Need to run ajax call to get credit card;
console.log("show dropdown");
console.log("there are saved cards and a selection was made");
poGetPaymentOption('credit card', parseInt($("#cc-dropdown-" + idx + " option:selected").val(),10), idx);
// this is the default card; display dropdown with default selected and static fields
console.log("supposedly after poGetPayment");
console.dir(cardInfo);
// why is this stuff running before poGetPaymentOtion finishes and returns a cardInfo object?
if( cardInfo.cc.cc_type == 'VI' ) { $('#cc_visaBig-'+idx).attr('class', 'cc_visaBig'); }
$('#cc-static-wrap-'+idx).show();
updateButtonState();
}
}
As you can see by the comments, the lines after the poGetPaymentOption call are running before that function is actually complete. I've verified this with logs in the poGetPaymentOption function as well (below).
function poGetPaymentOption(type, key, bIdx) {
if( type == 'credit card' ) {
console.log("signed in, credit card");
$.post('/site/ajax/customers/getSingleCreditCard',
{ 'key': key },
function(data) {
if( data.success == 1 ) {
console.log("poPayment success");
if(typeof cardInfo == 'undefined') {
cardInfo = new saveCardInfo(data);
}
} else {
console.log("poPayment no success");
}
}, 'json');
}
}
What I would expect to happen is for the call from showCreditCard to poGetPaymentOption to return success via the ajax call (which it does) and then create a new saveCardInfo object called cardInfo. It does happen, as far as I can tell, but the lines checking cardInfo.cc.cc_type and beyond are all happening before the object is created. I've attached a screenshot of my Firebug console so it's apparent the order things are happening.

What am I doing wrong? I need to make sure that poGetPaymentOption is fully complete and cardInfo has been created before continuing further in the showCreditCard function.