$.ajax, $.when 和 apply 的问题

Issues with $.ajax, $.when and apply

我遇到了 $.ajax$.whenapply 的问题。我有一个构造函数: ajax 请求在应该触发时未触发: http://plnkr.co/edit/Ul9d8tB7BHoZyHzzQQyB?p=preview (查看控制台)

function updateGeneralTerm() {
  return {
    id: "GeneralCondition",
    ajax: function () {
      return $.ajax({
        type: 'POST',
        url: "@Url.Action("UpdateGeneralTerms", "Agreements")",
        data: $("#GeneralConditions").serialize()
      })
    }
  }
}

//I inject it in my custom function        
function CustomFunction(arr) {
  let arrOfAjax = arr.map(function (obj) {
    return obj.ajax
  });
  $.when.apply(null, arrOfAjax);
}

CustomFunction([new updateGeneralTerm()];

在我的 CustomFunction 中,我正在检查其他内容,因为表单已更改...等等。但是它似乎与我的问题无关。没有任何反应。

将来我可能有 n 个特定的术语,只有当表格发生变化时我才想更新。

我的问题:ajax 没有被 $.when() 请求。如果我更改为 return obj.ajax(),ajax 请求将直接在那里触发,而不是由 $.when() 触发。 我希望 $.when() 能够处理所有 ajax 请求。

http://plnkr.co/edit/Ul9d8tB7BHoZyHzzQQyB?p=preview

尝试重写您的 CustomFunction 函数以使用 spread 运算符:

function CustomFunction(arr) {
  let arrOfAjax = arr.map(function (obj) {
    return obj.ajax
  });
  $.when(...arrOfAjax).then(function(...results) {
    console.log(results);
  });
}