Currently, I am in the process of developing my very first Titanium iPhone application.
Within a model module, I have this code snippet:
(function() {
main.model = {};
main.model.getAlbums = function(_args) {
var loader = Titanium.Network.createHTTPClient();
loader.open("GET", "http://someurl.json");
// Executes the specified function when the data is ready for processing
loader.onload = function() {
// Parse the JSON response
var albums = eval('('+this.responseText+')');
//alert(albums.length);
return albums;
};
// Send out the HTTP request
loader.send();
};
})();
and I invoke this function in a view section as follows:
(function() {
main.ui.createAlbumsWindow = function(_args) {
var albumsWindow = Titanium.UI.createWindow({
title:'Albums',
backgroundColor:'#000'
});
var albums = main.model.getAlbums();
alert(albums);
return albumsWindow;
};
})();
However, it appears that the invocation to the model (which retrieves data via HTTP) does not wait for a response. In the view, when I trigger the alert, the data from the model has not been received yet. What is the recommended way to handle this situation?
Thank you in advance