Page 1 of 2

executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 7:52 am
by Kartik A

I need help, I am executing below service and I have a alert msg inside the success clause of execute service and then I have alert msg outside the execute service. Ideally it should execute the service first and then call the second alert but instead its calling second alert first.

How to synchronize execute and subsequent statements in JS? Please advise. I read various similar question on this topic but couldnt get it working. I tried using deffered object.

What I am trying to achieve is by calling service I am populating variable dynARef and then add this variable value to 'a' tag by using statement $(this).find("div").find("a").html(dynARef) but the value is blank as its executing $(this).find("div").find("a").html(dynARef) and executeservice asynch manner. Please advise?

$.when (myservicename.execute({})
.done(function() {
alert("Refresh Data1");
});

myservicename.execute({"data":
{ "where": whereclausevalue },
"success": function( data ){
alert("Inside the success method of execute service");
dynARef = data[0].value;
}});

//and then I have one more alert msg out side execute serivce

alert("After execute service......");
$(this).find("div").find("a").html(dynARef);

assign some the value


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 7:54 am
by Kartik A

I need help, I am executing below service and I have a alert msg inside the success clause of execute service and then I have alert msg outside the execute service. Ideally it should execute the service first and then call the second alert but instead its calling second alert first.

How to synchronize execute and subsequent statements in JS? Please advise. I read various similar question on this topic but couldnt get it working. I tried using deffered object too but it didnt help...

$.when (myservicename.execute({})
.done(function() {
alert("Refresh Data1");
});

What I am trying to achieve is by calling service I am populating variable dynARef and then add this variable value to 'a' tag by using statement $(this).find("div").find("a").html(dynARef) but the value is blank as its executing $(this).find("div").find("a").html(dynARef) and executeservice asynch manner. Please advise?

myservicename.execute({"data":
{ "where": whereclausevalue },
"success": function( data ){
alert("Inside the success method of execute service");
dynARef = data[0].value;
}});

//and then I have one more alert msg out side execute serivce

alert("After execute service......");
$(this).find("div").find("a").html(dynARef);

assign some the value


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 10:09 am
by Maryna Brodina

Hello! Working on it. I'll update.


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 11:18 am
by Maryna Brodina

Invocation of the services is not synchronized. Run your code pre$(this).find("div").find("a").html(dynARef); /pre on servcice success event (where you have first alert and fill in dynARef variable)


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 2:56 pm
by Kartik A

It tried executing my code inside success event but its doesn't set the html. Were as same code work perfectly fine outside the success event. Could you please advise why below code is not working inside success event.

$(this).find("div").find("a").html(dynARef);

Thanks for your help Maryna!


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 4:43 pm
by Kateryna Grynko

Hi Kartik,

Please check the value of 'this' when running the code (F12). Does it contain needed tags?


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 5:24 pm
by Kartik A

If I put the below alert inside success event then it say undefine but when I put it outside it gives me required tag properly.

alert(" added new tag"+$(this).find("div").html());


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 5:25 pm
by Kartik A

Is this a bug $(this) doesn't work inside success event? Or I am missing something? Please advise?


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 6:56 pm
by Kateryna Grynko

Kartik,

Output this value in console please: preconsole.log(this);/pre


executing REST service from JS - how to synchronize

Posted: Tue Dec 17, 2013 7:36 pm
by Kartik A

I added below console inside and outside success event as shown below-

console.log("this value is outside success event : "+this);
console.log("this value is inside success event : "+this);

Following is printed on console-

this value is outside success event : [object HTMLDivElement]

this value is inside success event : [object Object]