简体   繁体   English

如何根据用户输入解析JQuery Promise

[英]How to resolve a JQuery promise based on the user input

I've got this code whose result should be returned as either resolved or rejected promise. 我有这段代码,其结果应作为已解决或已拒绝的承诺返回。

var runChecks = function(hasClickId, maxClicksModeIsNotOff) {
    var dfd = $.Deferred();

    clickIdCheckPassed = hasClickId; // equals false
    maxClicksCheckPassed = maxClicksModeIsNotOff; // equals false

    if (!clickIdCheckPassed) {
        clickIdCheckPassed = runClickIdCheck();
    }

    if (!maxClicksCheckPassed) {
        maxClicksCheckPassed = runMaxClicksCheck();
    }

    if (maxClicksCheckPassed && clickIdCheckPassed) {
        dfd.resolve('Resolved');
    } else {
        dfd.reject('rejected');
    }

    return dfd.promise();
};

Functions runClickIdCheck() and runMaxClicksCheck() collect user input and based on it, return either true or false . 函数runClickIdCheck()runMaxClicksCheck()收集用户输入,并基于该输入返回truefalse If both resulting values are true , I should resolve the promise, otherwise it should be rejected - hence the conditional: 如果两个结果值都为true ,则我应解决诺言,否则应将其拒绝-因此有条件的:

if (maxClicksCheckPassed && clickIdCheckPassed) {
    dfd.resolve('Resolved');
} else {
    dfd.reject('Rejected');
}

However, the problem is that the code does not wait for the user input and each time evaluates the promise as 'rejected' even before the user gave any input. 但是,问题在于该代码不等待用户输入,并且每次甚至在用户给出任何输入之前,都将承诺评估为“已拒绝”。

For reference, this is how runClickIdCheck function looks like: 供参考,这是runClickIdCheck函数的外观:

var runClickIdCheck = function() {
    bootbox.confirm('Are you sure?', function(result) {
        if (result) {
            return true;
        }
    });
    return false;
};

How I can make this code behave as expected, so that I evaluate the promise to 'resolve' only when the functions dependent on user input both return true? 如何使此代码按预期方式运行,以便仅在依赖于用户输入的函数都返回true时,评估对“解决”的承诺?

Edit: 编辑:

The expected UI behaviour is that the form should be submitted only if the user confirms the dialogue twice: 预期的UI行为是仅当用户两次确认对话时才应提交表单:

$form.submit(function(e) {
    e.preventDefault();
    $.when(runChecks(hasClickId, maxClicksModeIsNotOff)).then(
        function(status) {
            $form.submit()
        },
        function(status) {
            return false;
        }
    );
}

This is why I thought the promise inteface would do the job. 这就是为什么我认为Promise接口会完成这项工作的原因。 The promise shoudl be resolved if runClickIdCheck() and runMaxClicksCheck() evaluate to true . 如果runClickIdCheck()runMaxClicksCheck()评估为truerunMaxClicksCheck()解决承诺。

While it's possible to make this work with $.Deferred() , I doubt that it is worth it. 尽管可以使用$.Deferred()使其工作,但我怀疑是否值得。

I have implemented a promise-based solution prior to writing the following code, and turned out to be a lot longer and a lot less clear than just using the callback-based approach that bootbox offers. 在编写下面的代码之前,我已经实现了一个基于bootbox的解决方案,结果证明,与仅使用bootbox提供的基于回调的方法相比, bootbox要长得多,而且不清楚得多。

Keep it simple. 把事情简单化。

$("#form").submit(function (e) {
    var self = this;

    e.preventDefault();

    bootbox.confirm('Question 1', function (confirmed1) {
        if (!confirmed1) return;

        bootbox.confirm('Question 2', function (confirmed2) {
            if (!confirmed2) return;

            $.post(self.action, $(self).serialize()).done(function (data) {
                // Ajax post has finished successfully
            });
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM