Async function in Expression


                        
        
            Survey.StylesManager.applyTheme("defaultV2");
        

//Hashtable for countries information
var countriesInformation = {};
//The list of listeners
var isReadyCallbackList = [];
function setValueAndnotifyListeners(country, res) {
    countriesInformation[country] = res;
    for(var i = 0; i < isReadyCallbackList.length; i ++) {
        isReadyCallbackList[i](res);
    }
    isReadyCallbackList = [];
}
function getCountryInfoAjax(survey, country, isReadyCallback) {
    if (!country) {
        isReadyCallback({ isFound: false });
        return null;
    }
    var countryInfo = countriesInformation[country];
    if(!!countryInfo && (countryInfo.status == "processing")) {
        isReadyCallbackList.push(isReadyCallback);
        return null;
    }
    if(!!countryInfo  && countryInfo.status == "completed") {
        isReadyCallback(countryInfo);
        return countryInfo;
    }
    countriesInformation[country] = {status: "processing"};
    isReadyCallbackList.push(isReadyCallback);

    survey.setVariable("country_request_processing", true);
    $.ajax({
        url: "https://surveyjs.io/api/CountriesExample?name=" + country,
        type: "GET",
        success: function (data) {
            survey.setVariable("country_request_processing", false);
            if (!data || data.length < 1) {
                setValueAndnotifyListeners(country, {status: "completed", isFound: false});
                return;
            }
            var countryValue = data[0];
            setValueAndnotifyListeners(country, {status: "completed", isFound: true, officialName: countryValue.officialName, region: countryValue.region});
        },
        error: function (xhr, ajaxOptions, thrownError) {
            survey.setVariable("country_request_processing", false);
            setValueAndnotifyListeners(country, {status: "completed", isFound: false});
        }
    });
    return null;
}
function getCountryInfo(context, params, property) {
    if(params.length < 1) {
        context.returnResult(null);
    }
    var self = this;
    var isReady = function(countryInfo) {
        context.returnResult(countryInfo[property]);
    }
    return getCountryInfoAjax(context.survey, params[0], isReady);
}
function getCountryOfficialName(params) {
    return getCountryInfo(this, params, "officialName");
}
function getCountryRegion(params) {
    return getCountryInfo(this, params, "region");
}
function isCountryExist(params) {
    return getCountryInfo(this, params, "isFound");
}

Survey.FunctionFactory.Instance.register("getCountryOfficialName", getCountryOfficialName, true);
Survey.FunctionFactory.Instance.register("getCountryRegion", getCountryRegion, true);
Survey.FunctionFactory.Instance.register("isCountryExist", isCountryExist, true);

                
                    var json = {
  checkErrorsMode: "onValueChanged",
  elements: [
    {
      type: "html",
      name: "requesting",
      html: "The data is requesting",
      visibleIf: "{country_request_processing} = true"
    },
    {
      type: "text",
      name: "country",
      title: "Please enter the country name:",
      "placeHolder": "Estonia",
      validators: [
        {
          type: "expression",
          expression: "isCountryExist({country}) = true",
          text: "We could not find country with this name."
        }
      ]
    },
    {
      "type": "expression",
      "name": "officialName",
      "title": "Oficial name {country} is:",
      "expression": "getCountryOfficialName({country})",
      "visibleIf": "{officialName} notempty"
    },
    {
      type: "expression",
      name: "region",
      title: "{country} is located in region:",
      expression: "getCountryRegion({country})",
      visibleIf: "{region} notempty"
    }
  ]
};

                    window.survey = new Survey.Model(json);
                
        
            survey.onComplete.add(function(sender) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(sender.data, null, 3);
            });
        
        
            $("#surveyElement").Survey({
            model: survey 
            });
        

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Async function in Expression, jQuery Survey Library Example</title>
    
<meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/jquery"></script>
    <script src="/DevBuilds/survey-jquery/survey.jquery.min.js"></script>
    <link href="/DevBuilds/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">

</head>
<body style="margin: 0">
    
            <div id="surveyElement" style="display:inline-block;width:100%;">
            </div>
    <div id="surveyResult"></div>

<script type="text/javascript" src="./index.js"></script>

</body>
</html>

Why we use cookies.

This site uses cookies to make your browsing experience more convenient and personal. Cookies store useful information on your computer to help us improve the efficiency and relevance of our site for you. In some cases, they are essential to making the site work properly. By accessing this site, you consent to the use of cookies.

For more information, refer to DevSoft Baltic’ privacy policy and cookie policy.