jquery - Return promise to use with ajax object -


i have function pass form element in order handle form submits using ajax.

i want return ajax object handling form submit function can attach additional done callbacks -- problem don't know how return ajax object without creating it(and executing it) first.

how can substitute promise in lieu of actual ajax object return attach callbacks to?

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

edit: here i'd have happen function

 var formobject = $global.handleajaxform(element);                 formobject.ajax.done(function(data1) {                     if (data1.status == 'ok')                         window.location.href = (data.redirecttourl == null) ? "~/dashboard" : data.redirecttourl;                 }); 

try create own deferred

handlemodalformsubmit: function (element) {     var form,     modalcontainer = $(element).closest('.modal'),         modal = $(element).closest('.modal-dialog'),         ajaxdata, $deferred = jquery.deferred();       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');             }             $deferred.done.apply(this, arguments);         }).always(function (data) {             modal.spin(false);             modal.fadeto(500, 1);             $deferred.always.apply(this, arguments);         }).fail(function () {             $deferred.fail.apply(this, arguments);         });         modal.fadeto(300, 0);         modal.spin();     });     var returningobject = {         element: form,         ajax: ajaxdata     };     return $deferred.promise(); } 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -