Titanium:如何从此功能获取数据?

时间:2014-07-10 04:18:58

标签: javascript titanium appcelerator

当我在app.js中拥有所有这些代码时,我已经完成了所有工作,但是因为我将函数和窗口移动到他们自己的单独文件中,所以我很适合。

在main_Window.js中,我打开主窗口时设置一个事件监听器来运行一个函数并返回表数据。我知道该函数正在获取数据,因为我可以在控制台中将其打印出来,但它并没有将它传回我的事件监听器。

loadFeed.js的片段

//gets data and builds table rows
//I can see this in the console
Ti.API.info('tableData: ' + JSON.stringify(rowData));

//but it's not getting returned to my variable 
//in main_Window.js
return rowData;

主片段_Window.js

var newsFeed = require('loadFeed');

win.addEventListener("open",function(){
var tableData = newsFeed.loadFeed();

//now the console says that tableData is undefined  
Ti.API.info('tableData: ' + JSON.stringify(tableData));

tableView.setData(tableData);
});

app.js

Titanium.UI.setBackgroundColor('#000');

var be_url = 'http://192.168.1.200:10080';

//later we will get these values from user input
var usr_email =  'user1@wordout.tv';
var usr_password = 'fake_password';

//set from authenticate.js
var usr_session;

//create the main window below
var mainWin = Ti.UI.createWindow({
    title: 'Main Window',
    width: Ti.UI.Fill,
    height: Ti.UI.Fill,
    backgroundColor: '#fff',
    navBarHidden: 'true',
    url: 'ui/common/main_Window.js'
});

//validating email and password.  
//Storing userid in session and returning sessionid for future data calls.
//opens mainWin when successfully authenticated
var authorize = require('ui/common/authenticate');
authorize();

1 个答案:

答案 0 :(得分:0)

您应该执行以下操作:

修改你的loadFeed函数以接受回调并在回调中执行:

tableView.setData(tableData);

您正面临一个非常常见的问题,您(将)使用的ajax请求是异步请求。 因此控件不会等到你从服务器收到值。相反它首先执行return语句。对于使用commonjs方法,你必须在模块中添加一个回调,以便在从服务器接收数据时更新值。

检查documentation

你必须在onload函数中调用你的回调。

相关问题