Page 1 of 1

How can I execute a service synchronously?

Posted: Mon Jun 10, 2013 10:38 am
by w

I have this code in a button click event:

precode
for (var i=0;i<table&#46;length;i++){
localStorage&#46;setItem('syncContactObj', table);
READ_myService&#46;execute({});
}
/code/pre
In the request part of the myService, I have a javascript that uses localStorage.getItem('syncContactObj') to construct a filter.

But while the myService is executing with the first iteration of the for-loop, I suspect that the for-loop already continues and sets the next localStorage Item and initiates the next myService execute. But I use the localStorage Item in the myService success event, so when the 1st service is still executing, the localStorage item is already replaced with a new value of the 2nd for-loop iteration.
Which makes the code in the success event of the myService erronous, because it uses the localStorage item.

How can I make sure that the 1st service call is completed before the for loop continues with the next iteration?


How can I execute a service synchronously?

Posted: Mon Jun 10, 2013 10:58 am
by Kateryna Grynko

How can I execute a service synchronously?

Posted: Mon Jun 10, 2013 11:46 am
by w

Can you elaborate a bit more please, I have no clue on how to use it with the service.execute method, or do I have to use it in a success event, ... ?


How can I execute a service synchronously?

Posted: Mon Jun 10, 2013 12:07 pm
by Kateryna Grynko

You can find there all you need. You would need to develop your app yourself.
The code needs to be changed a bit. Then you should paste it in your code with the cycle.


How can I execute a service synchronously?

Posted: Mon Jun 10, 2013 12:10 pm
by Kateryna Grynko

You can surf "jquery deferred explained" in Google, read the descriptions and follow the tutorials. This is outside the scope of our support.


How can I execute a service synchronously?

Posted: Mon Jun 10, 2013 12:16 pm
by w

precode
for (var i=0;i<table&#46;length;i++){
localStorage&#46;setItem('syncContactObj', table&#46;contact_obj);

Code: Select all

     $&#46;when(myService&#46;execute({}))&#46;done( 
       function (){  

       } 
     ); 

}
/code/pre

This gives the same result. But if I put an alert in the function, then it works (because of the delay caused by the alert).

Is the service.execute a deferred object?


How can I execute a service synchronously?

Posted: Mon Jun 10, 2013 1:31 pm
by w

It seems to be that the only way to resolve this, is to pass the table.contact_obj to the myservice callback function instead of using localstorage.

How can I override the default success callback function when i call execute in Appery, so that it has 2 input parameters instead of only the 'data' now?


How can I execute a service synchronously?

Posted: Mon Jun 10, 2013 2:31 pm
by Kateryna Grynko

There is no way to do this.

You would need to save the array in localStorage.
Then create a counter variable.
On each Success event increase the counter variable value (+1).
Then call the service again with the new value from the array (by counter index).