Override the preventDefault () method

2

Is there any way to "undo" the preventDefault() method? I have this little code that solves this, but I wanted to know if there is any more efficient way to do it.

var $form = $('form');
$form.data('working', false);
$form.data('canSubmit', false);

$form.submit(function (e) {
    if (form.validations.form($form)) {
        if (!$form.data('working')) {
            $form.data('working', true);

            if ($form.data('canSubmit')) {
                // make the post here
            } else {
                e.preventDefault();
                $.get(BASE + 'file.txt', function (response) {
                    var lastPartnerSent = response;
                    var nextPartner = '';
                    switch (lastEmailSent) {
                        case 'Empresa 1':
                            nextPartner = 'Empresa 2';
                            break;
                        case 'Empresa 2':
                            nextPartner = 'Empresa 3';
                            break;
                        default:
                            nextPartner = 'Empresa 1';
                    }

                    $('input.i_parceiro').val(nextPartner);
                    $form.data('canSubmit', true);
                    $form.submit();
                });
            }
        }
    } else {
        $form.data('working', false);
        e.preventDefault();
        return false;
    }
});

This happens because I have to read and write a file after doing the field validations and only after that make the post for the page. The post can not be done by ajax.

The question is basically whether you can divide that form into 3 steps, ie:

1st validate fields, eg name, email, etc. (synchronous)

3rd submit the form per post

    
asked by anonymous 07.08.2015 / 12:23

1 answer

2

You can use .on() and .off() , that is, add the event observer and then remove if validation gives true .

The idea would be like this (notice pseudo-code to give you an idea how to do it):

$form.on('submit', verificador);

function verificador(e){
    if (!validouCerto) return e.preventDefault(); // ou outros avisos caso não valide

    // aqui código caso valide (pois o "if" anterior faria "return" caso não valide)
    $form.off('submit', verificador);
    $form.submit();
}

The reason for using $form.on('submit', verificador); is to be able to use .off() which has to indicate the same function so that it can be removed.

After the information you put in the question / chat and comments I suggest you use this way:

var $form = $('form');
$form.on('submit', verificador);

function verificador(e) {
    var validou = form.validations.form($form);
    e.preventDefault();
    if (!validou) return false;

    $.get(BASE + 'file.txt', function (response) {
        var lastPartnerSent = response;
        var nextPartner = '';
        switch (lastEmailSent) {
            case 'Empresa 1':
                nextPartner = 'Empresa 2';
                break;
            case 'Empresa 2':
                nextPartner = 'Empresa 3';
                break;
            default:
                nextPartner = 'Empresa 1';
        }

        $('input.i_parceiro').val(nextPartner);
        $form.off('submit', verificador);
        $form.submit();
    });
}
    
07.08.2015 / 12:33