Cart Licenses Contact Support Online Creator Log in/Register
Try
Free
v 1.9.43

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);
            });
        
        
            ReactDOM.render(
            <SurveyReact.Survey model={survey}  />, document.getElementById("surveyElement"));
        

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Async function in Expression, Reactjs Survey Library Example</title>
    
<meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/jquery"></script>
    <script src="https://unpkg.com/react@17.0.1/umd/react.production.min.js"></script>
    <script src="https://unpkg.com/react-dom@17.0.1/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.2.5/babel.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.core.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.i18n.min.js"></script>
    <script src="/DevBuilds/survey-react-ui/survey-react-ui.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/babel" src="./index.js"></script>

</body>
</html>

Tell Us What You Think

Help us serve you better by taking this brief survey.
We are interested to learn more about your
experience of using our libraries.

We'd really appreciate your feedback.

Start the Survey

Approximate time to complete: 2 min.