同步调用已弃用,但需要

时间:2018-07-13 20:07:29

标签: javascript jquery ajax

我有一个Web应用程序,在其中使用了第三方网格。网格在setCellValue中处理其单元格上的事件,如下所示:

    $("#productsGridContainer").dxDataGrid({
    dataSource: data,
    keyExpr: "Id",
    allowColumnResizing: true,
    showRowLines: true,
    showBorders: true,
    rowAlternationEnabled: true,
    sorting: true,
    paging: {
        pageSize: 10
    },
    pager: {
        showPageSizeSelector: true,
        allowedPageSizes: [10, 20, 50, 100]
    },
    editing: {
        mode: "row",
        allowUpdating: true,
        allowDeleting: true,
        allowAdding: true
    },
    columns: [
        {
            dataField: "ItemDesc",
            caption: "Description",
            validationRules: [{ type: "required" }]
        },
        {
            dataField: "ItemId",
            caption: "SKU",
            lookup: {
                dataSource: skus,
                displayExpr: "Value",
                valueExpr: "Key"
            },
      setCellValue: function(newData, value, currentRowData) {
        newData.ItemId = value;

        $.ajax({
            type: "POST",
            url: rootDir + "PaaSubmission/GetSkuItemData",
            data: '{ itemId: ' + value + ', custNo: ' + selectedCustomer.val() + ' }',
            contentType: "application/json; charset=utf-8",
            async: false, // if default/true, newData will not be defined in .done
            dataType: "json"
          })
          .done(function(data) {
            newData.ItemDesc = data.Description;
            newData.ItemSize = data.Size;
            newData.ItemOldPrice = data.OldPrice;
          })
          .fail(function(xhr, status, error) {
            displayError("The ajax call to GetSkuItemData() action method failed:",
              JSON.parse(xhr.responseText), "Please see the system administrator.");
          })
      }

如您所见,ajax调用完成后,设置newData属性为时已晚。 newData仍然被定义,但是设置它为时已晚,因为届时将执行setCellValue()。这使我的问题与其他类似的问题有所不同。因此,我必须使用async:false同步调用$ ajax()。但我知道它已被弃用,因为它会冻结浏览器。您能不能在不同步调用$ ajax()的情况下给我一个建议吗?

0 个答案:

没有答案