首先,我承认获得延迟管道 API 是最近对我来说最具挑战性的事情。
要求
如果一系列检查通过,我想提交一份表格。
其中一些将是具有同步客户端验证的函数,而另一些将是执行 AJAX 调用的异步检查。异步函数返回一个布尔回调值。
同步和异步函数都可能
confirm()
提示接受用户输入。用户输入将决定是否执行下一个块(如果用户选择取消确认对话框,则下一个块不应继续),因此不应提交表单。这个非常重要。仅当所有单独的块都返回(或回调)为真时,才应提交表单。
稍后添加 - 异步函数可能在某些条件下包含 ajax 调用,因此在某些用例中,可能无法完成 ajax 调用。
- 在异步函数内部,可以在 AJAX 响应处理代码中进行用户确认检查。
以下是我的功能结构 -
function synchronousMethod()
{
//some logic
return (confirm("user input"));
}
function aSynchronousMethod1(callback)
{
if(condition)
{
//ajax call
callback(bool); //does callback true/false
}
else
{//some use cases may follow this path and not make the AJAX call at all.
return true;
}
}
以下是我对 $.Deferred 的试验之一(可能是我做错了)-
$.when( synchronousMethod(), aSynchronousMethod1(),aSynchronousMethod2())
.done(function() {
//I want to submit my form if all the functions returned/called-back true
console.log("submitting form");
})
.fail(function() {
console.log( 'I fire if one or more requests failed.' );
});
但是文本“提交表单”在异步函数完成之前执行,并且任何同步函数也会导致此错误TypeError: callback is not a function
。
我观察到,如果我不将括号放在函数调用后的括号内,则任何函数都不会执行$.when
,即如果我保持这样 -
$.when( synchronousMethod, aSynchronousMethod1,aSynchronousMethod2)
但是没有任何功能执行。
更新
另一种方法
我也尝试过类似的方法,但与第一种方法有类似的问题 -
var promise = defer.pipe(synchronousMethod);
promise = promise.pipe(aSynchronousMethod1);
promise = promise.pipe(aSynchronousMethod2);
promise = promise.pipe($("form#frmCampaigns").submit());
更新 2 又增加了两点——
异步函数可能在某些条件下包含 ajax 调用,因此在某些用例中,可能无法完成 ajax 调用。
在异步函数内部,可以在 AJAX 响应处理代码中进行用户确认检查。
更新了异步函数的结构 -
function aSynchronousMethod1(callback)
{
//some sync logic here
if(condition) /*some condition*/
{
//ajax call
if(ajax sucess response)
{
callback((confirm("If user accepts"));
}
else
{
callback(false);
}
}
else
{//some use cases may follow this path and not make the AJAX call at all.
callback(true);
}
}