Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn promise to use with ajax object
    primarykey
    data
    text
    <p>I have a function that I pass a <code>form</code> element into in order to handle form submits using AJAX.</p> <p>I want to return the ajax object that will be handling the form submit from my function so that I can attach additional <code>done</code> callbacks -- the problem is I don't know how to return the ajax object without creating it(and executing it) first. </p> <p>How can I substitute a promise in lieu of the actual ajax object so that I return something to attach extra callbacks to?</p> <pre><code>HandleModalFormSubmit: function (element) { var form, modalcontainer = $(element).closest('.modal'), modal = $(element).closest('.modal-dialog'), ajaxdata; if (element.is('form')) form = $(element); else { form = element.find('form'); } $.validator.unobtrusive.parse(form); $(element).on('submit', function (event) { event.preventDefault(); ajaxdata = $.ajax({ type: form.method, url: form.action, data: $(form).serialize() }).done(function (data) { if (data.status == null) { modal.html(data); } else { modalcontainer.modal('hide'); }; }).always(function (data) { modal.spin(false); modal.fadeTo(500, 1); }); modal.fadeTo(300, 0); modal.spin(); }); var returningobject = { element: form, ajax: ajaxdata }; return returningobject; } } </code></pre> <p>EDIT: Here is what I'd like to have happen with the function</p> <pre><code> var formobject = $Global.HandleAjaxForm(element); formobject.ajax.done(function(data1) { if (data1.status == 'ok') window.location.href = (data.redirectToUrl == null) ? "~/Dashboard" : data.redirectToUrl; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload