views:

543

answers:

1

Hi,

I have the following code bound to the click event of a button:

function submitForm() {

    var $submitOK = false;
    $('#form1').ajaxSubmit({
        beforeSubmit: function() {
            $submitOK = $('#form1').valid();
            return $submitOK;
        },
        success: function(html, status) {
            $("#response").text(html.substring(1, html.length - 1));            
        }
    });

    if ($submitOK) {
        processForm1();
    }

    return $submitOK;
}

How does the timing of the ajaxSubmit and the beforeSubmit callback play out - will the beforeSubmit callback always return before execution returns from the ajaxSubmit()?

The code behaves as desired - returning $submitOK equal to the result of $('#form1').valid(), but I wanted to make sure this is deterministic behaviour.

Thanks.

A: 

Yes, beforeSubmit will always return first, that's it's purpose - it is a pre-submission hook.

karim79
Thanks, I guessed as much. I knew the ajax submission doesn't happen if the beforeSubmit returns false, just wanted confirmation that the code following the ajaxSubmit would also not be reached until the beforeSubmit callback had returned.
TonE