Titanium HTTPClient返回的速度太快 [英] Titanium HTTPClient returns too 'fast'

查看:102
本文介绍了Titanium HTTPClient返回的速度太快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能:

  getTasks: function()
    {
        var taskRequest = Titanium.Network.createHTTPClient();
        var api_url = 'http://myawesomeapi.heroku.com/users/' + Ti.App.Properties.getString("userID") + '/tasks';

        var tasks = [];
        taskRequest.onload = function() {
            var response = JSON.parse(this.responseText), 
            len = response.length,
            i = 0,
            t;

            for(; i < len; i++)
            {
                task = response[i];
                var newTask = {};
                newTask.rowID = i;
                newTask.title = task.title;
                newTask.description = task.description;
                newTask.id = task.id;
                newTask.hasChild = true;

                tasks.push(newTask);
            }

            alert(tasks);
        }

        taskRequest.open('GET', api_url, false);
        taskRequest.setRequestHeader('Content-Type', 'application/json');
        taskRequest.send();

        alert(tasks);
            // return tasks;
    }

此功能在我的控制器中;我需要在其中加载数据时在视图中调用它.但是,我希望将此数据return分配给视图中的变量.

This function is in my controller; I call it in my view when I need to load the data in. However, I wish to return this data so I can assign it to a variable in the view.

现在发生的事情是它返回了空度.最后一个警报(底部警报)似乎运行得太快,它返回一个空数组,而只有在onload函数完成后才得到警报的警报包含了我所需要的.

Now what happens is that it returns emptiness. The last alert (bottom one) seems to be running too fast and it returns an empty array, while the one that only gets alerted after the onload function is done, contains what I need.

现在我的问题很明显,我如何才能让函数返回带有数据的数组,而不是没有数据?

Now my obvious question, how can I get my function to return the array with the data, instead of without?

设置计时器似乎不是正确的决定..谢谢!

Putting a timer on it seems hardly the right decision.. Thanks!

推荐答案

但是,我希望返回此数据,以便可以将其分配给视图中的变量."

除了使AJAX请求同步"(您可能不希望这样做)之外,没有任何方法可以返回数据.

Aside from making the AJAX request synchronous (which you probably don't want), there isn't any way to return the data.

任何依赖于响应的代码都需要在响应处理程序中调用.

Whatever code relies on the response needs to be called from within the response handler.

由于可以传递函数,因此您可以让getTasks方法接收一个回调函数,该回调函数将被调用并将接收tasks数组.

Since functions can be passed around, you could have your getTasks method receive a callback function that is invoked and will receive the tasks Array.

  getTasks: function( callback ) // receive a callback function
    {
        var taskRequest = Titanium.Network.createHTTPClient();
        var api_url = 'http://myawesomeapi.heroku.com/users/' + Ti.App.Properties.getString("userID") + '/tasks';

        taskRequest.onload = function() {

            var tasks = [];

            // code populating the tasks array

            alert(tasks);

            callback( tasks ); // invoke the callback
        }

        taskRequest.open('GET', api_url, false);
        taskRequest.setRequestHeader('Content-Type', 'application/json');
        taskRequest.send();
    }


所以您将像这样使用它...


So you'd use it like this...

myObj.getTasks(function(tasks) {
    alert('in the callback');
    alert(tasks);
      // Any and all code that relies on the response must be
      //   placed (or invoked from) inside here
    some_other_function();
});

function some_other_function() {

    // Some more logic that can't run until the tasks have been received.
    // You could pass the tasks to this function if needed.

}

这篇关于Titanium HTTPClient返回的速度太快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆