简体   繁体   English

阻止事件在deferred.reject上继续

[英]Stop event from continuing on deferred.reject

Hoping some of you may help me with this problem. 希望你们中的一些人可以帮助我解决这个问题。

I have a few navigation link on top of my application which have active and not-active state. 我的应用程序顶部有一些导航链接,它们具有活动和非活动状态。 In theory, I want to jump from one to another and if there exists a form which isn't complete/valid, I trigger validation errors and stay on the same page. 从理论上讲,我想从一个跳到另一个,并且如果存在一种形式不完整/无效的表格,我会触发验证错误并停留在同一页面上。 What is happening in my case is form validation works fine but navigation links on top change state from non-active to active, whichever was clicked. 在我的情况下,表单验证工作正常,但是顶部的导航链接从非活动状态更改为活动状态,无论单击哪个状态。

I have a ValidateForm function that validates and submits the form if its is valid, else it returns deferred.reject(); 我有一个ValidateForm函数,该函数可以验证并提交表单(如果表单有效),否则返回deferred.reject();。

function ValidateForm(submitAnyway) {
    var deferred = $.Deferred();
    var form = $('form');

    // if form doesn't exist on the page - quit
    if (typeof form[0] === "undefined") return true;       


    // now check for any validation errors
    if (submitAnyway) {
        if (!$(form).valid()) {                
            deferred.reject();
        } else {
            $(form).submit();
            deferred.resolve();
        }
    } else {
        deferred.resolve();
    }

    return deferred.promise();
}

I have a click event for those top navigation links as below: 对于这些顶部导航链接,我有一个点击事件,如下所示:

var DonutsClickEvent = function (e, arg) {
    var url = $(this).attr('data');
    var data = { param: arg };

    if (typeof url === "undefined") return false;

    $.when(window.ValidateForm(false)).then(
        function () {
            LoadPartialView_Main(url, data);                
        },
        function(){
            return false; // I'm trying to return false here to stop event from continuing 
        }
    );  
    Console.log("This statement runs before $.when completes");
    // and event continues and the clicked nav button changes 
    // its state from non-active to active, even though form doesn't validate
}

I recently added $.Deferred functionality to my code due to some events firing with messed up sequence... Before my validateForm method would return true or false and based on that i'd continue executing event if true, if false i'd stop and it was all good. 我最近在代码中添加了$ .Deferred功能,因为某些事件触发了混乱的序列...在我的validateForm方法返回true或false之前,基于此我将继续执行事件(如果为true,如果为false),我将停止一切都很好。

Not sure what am I doing wrong here. 不知道我在做什么错。 I'd appreciate any kinda help. 我将不胜感激。

Thanks! 谢谢!

Johny 强尼

You can't asynchronously decide whether you want to block the default action or not. 您无法异步决定是否要阻止默认操作。 By the time you get your async result, your function has already returned and the default action has already occurred. 到获得异步结果时,您的函数已经返回,并且默认操作已发生。

If you have to validate asynchronously, then you will have to always block the default action. 如果必须异步验证,则必须始终阻止默认操作。 If the validation succeeds, then you will have to manually carry out the desired action with Javascript. 如果验证成功,那么您将必须使用Javascript手动执行所需的操作。

So, assuming that LoadPartialView_Main() is what you want to have happen when validation succeeds, you can do something like this: 因此,假设LoadPartialView_Main()是验证成功后想要发生的事情,则可以执行以下操作:

var DonutsClickEvent = function (e, arg) {
    var url = $(this).attr('data');
    var data = { param: arg };

    if (typeof url === "undefined") return false;

    window.ValidateForm(false).then(function () {
        LoadPartialView_Main(url, data);
    }, function(err){
        // probably show validation error message to the user here
    });  
    // always prevent default action
    return false;
}

Also, there's no reason to use $.when() with a single promise. 另外,没有理由将$.when()与单个诺言一起使用。 You can just use window.ValidateForm(...).then(...) . 您可以只使用window.ValidateForm(...).then(...)

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

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