JavaScript on Appcelerator. Extract data from callback function
Titanium SDK version: 1.6.1
iP开发者_C百科hone SDK version: 4.2I am developing a Titanium Appcelerator app.
I got a function in a separate file that returns a section for a table view (http://pastie.org/1734554) and on the main file I got a call to this function with a callback. I want to be able to extract the callback data and add it to an array (http://pastie.org/1734548) but I cannot get that data out of the calling function. How is it done?
You’re running into the asynchronous nature of AJAX. Move your alert to within the callback function, and it’ll work as expected:
var rowData = [];
rowData.push("THIS ADDS TO THE ARRAY");
loadPhones(function(data) {
rowData.push(data);
alert(rowData);
});
The reason you have to pass a function to loadPhones
is that you don’t want the browser to lock up while you’re retrieving the list of phones. The way you had written it, the anonymous callback function had not been executed by the time you got to the alert
statement.
Do whatever you need to do with the retrieved data inside the loadPhones callback.
精彩评论