PDA

View Full Version : Form Synchronous Submit



fily55
22 Jul 2013, 12:21 AM
Hi,

I have a card layout and I don't want to move card if the form has failed to submit. Right now my code looks a little like this:



proceed = true;

form[i].submit({
success: function(form, action){
//do success
}, failure: function(form, action){
// don't move card
proceed = false;
}
});

if(proceed){
moveCard();
}



As you can see the problem with this is that the form submits asynchronously therefore by the time that proceed has been set to false in failure, the code has already tried to move card.
Is there any way I can make a form synchronous call? There might be a better solution that I am unaware of.

Thanks in advance for any help.

Farish
22 Jul 2013, 12:32 AM
one simple solution would be to place your moveCard() function inside the success case

fily55
22 Jul 2013, 12:38 AM
one simple solution would be to place your moveCard() function inside the success case

The reason I can't do this is because some cards might have more than one form or none at all.

EPV
22 Jul 2013, 2:00 AM
Try something like this (not tested).

Good luck / E




sendForms:function(formsArray) {
this.proceed = true;
this.numForms = formsArray.length;
this.processedForms = 0;

for(var i=0:i<formsArray.length;i++) {
form[i].submit({
scope:this,
success: function(form, action){
this.setStatus(true);
},
failure: function(form, action){
this.setStatus(false);
}
});
}
},

setStatus:function(status) {
// Keep track of number of forms submitted
this.processedForms++;

// Cannot go from false to true!
if(status===true && me.proceed!==false) {
me.proceed=true;
}else if(status===false) {
me.proceed=false;
}

if(this.processedForms===this.numForms) {
this.changeCard();
}
},

changeCard:function() {
if(this.proceed===true){
// Change card
}else {
// Handle errors
}
}

fily55
22 Jul 2013, 2:44 AM
Try something like this (not tested).

Good luck / E




sendForms:function(formsArray) {
this.proceed = true;
this.numForms = formsArray.length;
this.processedForms = 0;

for(var i=0:i<formsArray.length;i++) {
form[i].submit({
scope:this,
success: function(form, action){
this.setStatus(true);
},
failure: function(form, action){
this.setStatus(false);
}
});
}
},

setStatus:function(status) {
// Keep track of number of forms submitted
this.processedForms++;

// Cannot go from false to true!
if(status===true && me.proceed!==false) {
me.proceed=true;
}else if(status===false) {
me.proceed=false;
}

if(this.processedForms===this.numForms) {
this.changeCard();
}
},

changeCard:function() {
if(this.proceed===true){
// Change card
}else {
// Handle errors
}
}



Perfect! Your methodology works great. The only problem is that there is a chance that no forms are to be submitted. To fix this I added a simple if statement to your code.


sendForms:function(formsArray) {
this.proceed = true;
this.numForms = formsArray.length;
this.processedForms = 0;

for(var i=0:i<formsArray.length;i++) {
form[i].submit({
scope:this,
success: function(form, action){
this.setStatus(true);
},
failure: function(form, action){
this.setStatus(false);
}
});

// if no forms exists change card
if( formsArrray.length === 0) {
//change card
}
}
},

Thanks for the help, very useful