For an iPad app using a UIWebView, I'm passing a callback function to the app in an URL:
function query(db, query, callback) {
var iframe = document.createElement("IFRAME");
// Filter comments from the callback (as this would break things).
var callbackstr = "" + callback;
callbackstr = callbackstr.replace(/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g, '');
// Put the query + the callback in an url that will be caught by the iOS app.
iframe.setAttribute("src", "ios-query:#iOS#" + query +":#iOS#"+ callbackstr);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
The app parses the callback function from the URL, and calls the callback function with some data inserted through stringByEvaluatingJavaScriptFromString. This is all working fine.
However, now I want to use a closure in the callback function, like so:
var callback = function (problemdata) {
// Return the 'real' callback.
return function (tx, results) {
// Do something with problemdata
}
}(problemdataFromScopeChain)
This is problematic. Since the callback function gets converted to a string, all scope-chain information is lost.
Any suggestions on how to solve this problem?
edit:
I would prefer a solution on the side of the 'query' function. For example: Is there any way to convert the vars in the scope-chain to an eval()-able string?