Hiya,
I know this is a bit old, so, sorry for the necro, but this is an area that really plagued me for an extremely long time when working with call-backs inside SRF JS.
Here is a method that I've found that works quite well:
If you need to make multiple calls from different sources, or if you find yourself using callback functions a lot inside of PAM, a 'wrapper' function is a good way to keep your code a little more readable.
What you do is basically this:
{
dataSetWrapper : function(path,handler){
/*
utility function - passes a Datasetpath to a 'handler' function to allow treating data in
a dataset as if it were just a local JSON object.
*/
var callBack = new Object();
callBack.onSuccess = function(result)
{
var dataValue = ca_pam_convertToJavaScriptObject(result);
handler(dataValue)
}
}
callBack.onFailure = function (caught){
alert("Data Not Found For:\n"+path)
console.log(path)
console.log(handler)
}
ca_pam_getDatasetData(path, callBack);
}
}
This lets you build a generic function to just interact with whatever data you want. No you just call ca_fd.js.dataSetWrapper and then pass whatever function you want to execute on the dataset contents as an argument -
If you want to nest multiple callback operations, just call the wrapper again from inside your handler function, and pass it the next handler function in your chain.
I even went so far as to extend this function to take an optional third argument for scenarios in which I wanted to pass additional information to my handler.
Some JS wizards out there might be better at dealing with callbacks then I am, and have other input to give here, but I found this method to suit my needs pretty well, so I thought I'd share it in case anyone else was struggling.