Titanium HTTPClient 返回太“快" [英] Titanium HTTPClient returns too 'fast'

查看:26
本文介绍了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;
    }

这个功能在我的控制器里;当我需要加载数据时,我会在视图中调用它.但是,我希望返回这个数据,以便我可以将它分配给视图中的一个变量.

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 方法 receive 一个回调函数被调用并接收 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天全站免登陆