PDA

View Full Version : How to know when Ajax request completed inside a function?



dbassett74
16 Aug 2010, 12:47 PM
I have an Ajax Request contained within a function called getEmployees().

From another function, I have something like:

var records = getEmployees()

This doesn't seem to be working as it seems that var records is getting assigned before the Ajax Request in getEmployees is finished.

I realized that trying to assign the results form an Ajax Request to a variable is probably wrong, but I'm not sure how to implement the appropriate callback here. Somehow I need to know when the Ajax Request within getEmployees has completed OUTSIDE of the function. I know inside because I'm using onSuccess. But how do you know this from OUTSIDE?

Any help would be appreciated.

Animal
16 Aug 2010, 1:47 PM
You have been around long enough to know surely what the "A" in Ajax stands for.

The function will have to be



getEmployees(processRecievedEmployees);


where processReceived employees is your function which..... processes the received employees. You already have well designed code separated out into functions, and not a monolith, I assume, so this should be easy for you.

dbassett74
16 Aug 2010, 2:04 PM
You're an Animal! Okay, that works, but now there seems to be a scoping issue that I'm not sure how to handle. In the processReceivedEmployees() function, it refer to "this" to add a tab (it is a TabPanel). But it keeps complaining that "this.add" is not a function. How do I specify the scope to refer to the origional component?

Animal
16 Aug 2010, 2:18 PM
You can pass the scope in as well as the function. See how all Ext methods which take a callback work. This is what you are doing.

This is the stuff you need to know about Javascript.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function#Methods_2