简体   繁体   English

Titanium Studio保持功能执行,直到另一个功能完成

[英]Titanium Studio hold function execution until another function completes

Hey all I'm trying to get a window populated with a table view that is populated from a network function in Titanium Studio, build: 2.1.1.201207271312. 嘿,我正在尝试获取一个用表格视图填充的窗口,该表格视图是从Titanium Studio(内部版本:2.1.1.201207271312)中填充的。 I have the data being fetched properlybut the problem is that the program continues to run without waiting for the table view to be populated properly. 我已经正确提取了数据,但问题是该程序继续运行,而无需等待表视图被正确填充。 Here's the code: 这是代码:

ui.js: ui.js:

bs.ui.createTransitRoutesListWindow = function() {

    var winbsRoutesList = Titanium.UI.createWindow({
    });
    var tv2 = Ti.UI.createTableView();
    tv2 = bs.ui.createbsRouteListTableView();

    winbsRoutesList.add(tv2);       
};

bs.ui.createbsRouteListTableView = function() {

    var tv = Ti.UI.createTableView();       

    Ti.API.info('populating data');
        var busStopList = bs.db.routeStopList(); 

        tv.setData(busStopList);

        return tv;

};

db.js: db.js:

 bs.db.routeStopList = function() {
var stoplist = [];

bs.net.getRoutes(function(data) {
    Ti.API.info('data length: '+data.length);
    for (var i = 0;i<data.length;i++) {
        stoplist.push({
            title:data[i].stopName,
            id: i
        });
    }

});
return stoplist;
 }

network.js network.js

bs.net.getRoutes = function(_cb) { 

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onload = function() {  
        _cb(JSON.parse(this.responseText));

    Ti.API.info(this.responseText)
    };
    xhr.onerror = function(event) {
    }
    xhr.open("GET","<URL to valid JSON>", true);  
    //+ Ti.App.Properties.getString('currentBus','This is a string default')
    xhr.send();

};

bussearch.net.getRoutes() is an AJAX operation and thus it is asynchronous . bussearch.net.getRoutes()是AJAX操作,因此是异步的 This means that the code will NOT wait for it to complete. 这意味着代码不会等待它完成。 The code will proceed while the response is going. 响应进行时,代码将继续执行。 The time it will respond is not known also. 响应时间也不知道。

If you want to do something after the data returns, you should do everything in the callback instead or create deferred objects like jQuery (which are basically callback containers). 如果你想要做数据返回后的东西,你应该做的一切的回调,而不是或创建像jQuery递延对象(基本上回调容器)。

//db.js
bussearch.db.routeStopList = function(callback) {
    var stoplist = [];
    bussearch.net.getRoutes(function(data) {
        ....
        callback.call(this,stoplist);
    });
}

//ui.js
bussearch.ui.createBussearchRouteListTableView = function(callback) {
    var tv = Ti.UI.createTableView();       
    Ti.API.info('populating data');
    bussearch.db.routeStopList(function(busStopList){
        tv.setData(busStopList);
        callback.call(this,tv);
    }); 
};

//calling createBussearchRouteListTableView()
createBussearchRouteListTableView(function(tv){
    //tv in here is the data
    //do inside here what you want to do to tv after it's retrieved
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM