javascript - I need help finding an alternative to synchronous jQuery ajax -


i have complex form contains multiple tabs. each tab contains unique plupload instance (for uploading multiple images). form allows user upload medical image 'case' each case made of multiple imaging 'studies' (e.g. ct scans) , each study contains multiple images.

when user clicks 'submit' button, intercept click jquery because need following:

  1. check required fields entered [easy]
  2. get unique id number server. id number required each plupload instance know directory upload to.

in function called upon form submission have following code snippet:

var case_id;  // code check required fields entered ....  // case id number server $.get('ajax/unique-case-id').done(function(data){     case_id = data; });  // case_id , other things. must happen after ajax call ....  // if there problem uploading images, stop form submitting if (problem_occured) {     return false; } 

with current logic, need script pause until gets case_id. possible before jquery 1.8 $.ajax() async : false property has been deprecated.

my question two-fold:

  1. is there way hold script until required case_id?
  2. if not, idea how change logic work around this?

you might wondering why case_id important. plupload instances upload before form submits , need directory upload to. want images being uploaded go folder on server called case_id. let php script on server figure out them once gets rest of form posted data.

this common 'problem' can solved pretty using callbacks appropriately.

$("#submitbutton").click(function (event) {     event.preventdefault(); //don't submit form, we'll submit manually.      var case_id;      // code check required fields entered     ....      // case id number server     $.get('ajax/unique-case-id').done(function(data){         case_id = data;          // case_id , other things. must happen after ajax call         ....          // if there problem uploading images, stop form submitting         if (problem_occured) {             alert("something went wrong");         } else {             $("#referencetotheform").submit();         }      }); }); 

long story short, keeping "deal problems or submit form" inside of callback $.get call cause script 'pause' until gets data back. can use spin.js give user waiting experience until it's done.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -