In my script, I declare my ultimate target function:
function ahe_receiveNewLocation(itemType, itemId, itemName, locationId, locationName) {
// code goes here
}
My working function (the one that receives the callback request has a signature like this:
function ChooseTaxonomyItem_show(itemId, itemDescription, locationId, locationDescription, callbackFunction) {
// code goes here
The original calling function has to call the working function, passing in the callback function as a variable:
ChooseTaxonomyItem_show(-1, '', locationId, locationDescription, ahe_receiveNewLocation);
Notice the fifth argument. It looks like any other variable, but it is the name of the function to call. In Javascript, functions are 'first class objects' like any other object, and may be passed around as such.
But: The working function has to know the arguments back to the original function. This information is not passed with the function call. The usage of the callback function looks like this:
callbackFunction(itemType, itemId, itemName, locationId, locationName);
Notice that callbackFunction is the name of the variable that received the call back function name above.
Nice, huh?
No comments:
Post a Comment