停止事件继续deferred.reject [英] Stop event from continuing on deferred.reject

查看:97
本文介绍了停止事件继续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.

我有一个ValidateForm函数,如果表单有效则验证并提交表单,否则返回deferred.reject();

I have a ValidateForm function that validates and submits the form if its is valid, else it returns 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
}

我最近为我的代码添加了$ .Deferred功能,因为一些事件被搞乱了序列...在我的validateForm方法返回true之前或者基于我会继续执行事件,如果是真的,如果是假我会停止并且一切都很好。

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.

不确定我在这里做错了什么。我很感激任何帮助。

Not sure what am I doing wrong here. I'd appreciate any kinda help.

谢谢!

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.

如果您必须异步验证,那么您将不得不始终阻止默认操作。如果验证成功,那么您将不得不使用Javascript手动执行所需的操作。

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.

因此,假设 LoadPartialView_Main()是您在验证成功时想要发生的事情,您可以执行以下操作:

So, assuming that LoadPartialView_Main() is what you want to have happen when validation succeeds, you can do something like this:

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;
}






此外,没有理由使用 $。when()并使用单个承诺。你可以使用 window.ValidateForm(...)。然后(...)


Also, there's no reason to use $.when() with a single promise. You can just use window.ValidateForm(...).then(...).

这篇关于停止事件继续deferred.reject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆