0

How do I use holdReady along with doc ready? I need to make sure that holdReady gets fired before doc.ready, I am trying below but no luck as loadTab3() is still getting fired before storeContentList?

$.holdReady(true);

tab3Data = storeContentList('getArticleTypelistById', 'atid', '88', '5', '123');

$.holdReady(false);

$(document).ready(function () {
   //  tab3Data gets used in loadTab3 function below.
   loadTab3();
});


 function storeContentList(webServiceName, parameterName, parameterValue, noOfItems, IDstoExclude) {
        var inputParameters = webServiceName.toLowerCase() + ':' + parameterName.toLowerCase() + ':' + parameterValue + ':noofitems:' + noOfItems + ':excludeids:' + IDstoExclude;
        var clientcode = getCryptoToken(inputParameters);
        eval("data={" + parameterName.toLowerCase() + ":" + parameterValue + ", noofitems:" + noOfItems + ", excludeids:'" + IDstoExclude + "',clientcode:'" + clientcode + "' }");
        $.ajax({
            url: 'https://abc/Service.svc/' + webServiceName,
            dataType: 'jsonp',
            async: false,
            data: data,
            success: function (data) {
                //tab3Data = JSON.stringify(data);
                tab3Data = data;
                console.log('tab3Data in storeContentList function:' + tab3Data);
                return data;
            },
            error: function (data) {
                console.log('err occured');
            }
        });
    }

1 Answers1

0

Don't use async: false as an AJAX option.

... never. ever. Synchronous AJAX requests are bad. They lock up the browser. There is hardly ever a reason to use them. Avoid them at all costs. I cannot emphasise this enough.


I discuss it in detail here, but the TL;DR is that JSONP, as a transport, does not support synchronous requests.

The ideal solution would be to do away with $.holdReady completely, and embrace the asynchronous nature of AJAX. Basically, your async: false option is being ignored, and the AJAX request is happening asyncronously.

$(document).ready(function () {
    storeContentList();
});


 function storeContentList(webServiceName, parameterName, parameterValue, noOfItems, IDstoExclude) {
        var inputParameters = webServiceName.toLowerCase() + ':' + parameterName.toLowerCase() + ':' + parameterValue + ':noofitems:' + noOfItems + ':excludeids:' + IDstoExclude;
        var clientcode = getCryptoToken(inputParameters);
        eval("data={" + parameterName.toLowerCase() + ":" + parameterValue + ", noofitems:" + noOfItems + ", excludeids:'" + IDstoExclude + "',clientcode:'" + clientcode + "' }");
        $.ajax({
            url: 'https://abc/Service.svc/' + webServiceName,
            dataType: 'jsonp',
            data: data,
            success: function (data) {
                loadTab3(data); // Now pass data as an parameter to `loadTab3`; modify your code accordingly.
            },
            error: function (data) {
                console.log('err occured');
            }
        });
    }

Then change your declaration of loadTab3 from function loadTab3() to function loadTab3(tab3Data).

However, as you need to persist the response in a variable, the easiest (and most transparent way for the rest of your application) to do this would be to continue to use $.holdReady like so;

$.holdReady(true);

var tab3Data;

storeContentList('getArticleTypelistById', 'atid', '88', '5', '123');

$(document).ready(function () {
   //  tab3Data gets used in loadTab3 function below.
   loadTab3();
});


 function storeContentList(webServiceName, parameterName, parameterValue, noOfItems, IDstoExclude) {
        var inputParameters = webServiceName.toLowerCase() + ':' + parameterName.toLowerCase() + ':' + parameterValue + ':noofitems:' + noOfItems + ':excludeids:' + IDstoExclude;
        var clientcode = getCryptoToken(inputParameters);
        eval("data={" + parameterName.toLowerCase() + ":" + parameterValue + ", noofitems:" + noOfItems + ", excludeids:'" + IDstoExclude + "',clientcode:'" + clientcode + "' }");
        $.ajax({
            url: 'https://abc/Service.svc/' + webServiceName,
            dataType: 'jsonp',
            data: data,
            success: function (data) {
                tab3Data = data;

                $.holdReady(false);
            },
            error: function (data) {
                console.log('err occured');
            }
        });
    }
Community
  • 1
  • 1
Matt
  • 72,564
  • 26
  • 147
  • 178
  • But i do need to store the data in a variable and get it out from "Ajax Success" so that i can re use it. is there a way? – user3045352 May 08 '14 at 11:16