- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下功能:
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;
}
这个函数在我的 Controller 中;当我需要加载数据时,我会在我的 View 中调用它。但是,我希望返回
此数据,以便我可以将它分配给 View 中的变量。
现在发生的是它返回空虚。最后一个警报(底部的警报)似乎运行得太快,它返回一个空数组,而仅在 onload 函数完成后才收到警报的警报包含我需要的内容。
现在我的明显问题是,如何让我的函数返回包含数据的数组,而不是没有数据?
设置计时器似乎不是正确的决定。谢谢!
最佳答案
"However, I wish to return this data so I can assign it to a variable in the view."
除了使 AJAX 请求同步(您可能不想要)之外,没有任何方法可以返回数据。
任何依赖于响应的代码都需要从响应处理程序中调用。
由于函数可以传递,您可以让您的 getTasks
方法接收一个回调函数,该函数被调用并将接收 tasks
数组.
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();
}
所以你会像这样使用它......
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.
}
关于javascript - Titanium HTTPClient 也返回 'fast',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9056587/
我有一个 mysql 数据库,用户可以在其中输入文本。然后他们需要能够搜索此文本。我刚刚实现了 mysql 全文搜索,它确实使搜索速度快了很多。 然而,毫不奇怪,它使插入变慢了。但令我惊讶的是速度慢了
我在 Linux 3.15 机器上写了一个 TCP 客户端,它能够使用 TCP Fast Open: status = sendto(sd, (const void *) data,
“free(): invalid next size (fast)”中的“fast”或“normal”是什么意思:谁能解释一下这是什么意思/暗示或在哪里可以找到答案? 最佳答案 您看到的错误消息表明在
像 FAST 这样的数据编码协议(protocol)非常巧妙地减少了需要发送的数据量。本质上,一个人得到一个 char*,读取前几个字节作为整数会给你一个 ID 号,它指向你如何解码其余字节的说明(即
语境 我非常喜欢Roy Osherove所说的“快速集成测试”。这是集成测试,它可以: 严格在您的开发箱上执行。无需单独的环境。 尽管正在进行集成测试,但此类测试通常是通过您的单元测试工具(NUnit
我的代码中有一些子例程,我需要测量它们的执行时间。让我们假设例程在极端情况下每秒被调用 10-100 次。在 Fortran 中有许多方法可以测量时间,但由于调用的频率,我需要一种开销最低的方法。 时
我的电脑中的这段代码在java中执行了1秒,但在C中执行了20多秒。java是如何执行的? int a[] = new int[50000] ; for(int i = 0 ; i < 50000 ;
我用 fastai.tabular 训练了一个模型。现在,我有一个合适的学习器。最终,模型将应用于新数据,而不仅仅是在训练集上拟合并在测试集上进行评估等。我尝试了不同的方法,所有这些都导致了错误或一些
当我曾经对嵌入式系统和早期 8/16 位 PC(6502、68K、8086)进行编程时,我对每条指令执行所需的确切时间(以纳秒或微秒为单位)有很好的把握。根据系列的不同,一个(或四个)周期相当于一次“
让我立即澄清一下这个听起来很温和的标题。这实际上已经困扰我很长一段时间了,尽管感觉这是一个非常基本的问题。 许多语言让开发人员玩弄位,从而给人一种效率错误的印象,例如 bool.h据我了解,C hea
我有一个代码。 private static String generateString(int size) { StringBuffer s = new StringBuffer();
[简短回答:糟糕的基准测试方法。你会认为我现在已经想通了。] 问题表现为“找到一种快速计算 x^y 的方法,其中 x 和 y 是正整数”。典型的“快速”算法如下所示: public long fast
我必须乘以 2(大部分时间)稀疏矩阵。这些矩阵相当小(大约 10k*10k),我有两个至强四核和一个线程来完成这项工作? 是否有任何用于多线程 moltiplication 的快速库?还有其他建议吗?
我正在对约 40K 文档的集合执行 where in box 查询。查询耗时约 0.3 秒,获取文档耗时约 0.6 秒(结果集中约有 10K 文档)。 文档相当小(每个约 100 字节),我限制结果只
我正在寻找 4 个变量的标量函数的局部最小值,并且我对变量有范围约束(“框约束”)。函数导数没有封闭形式,因此需要解析导数函数的方法是不可能的。我已经用 optim 尝试了几个选项和控制参数功能,但所
我正在尝试部署一个使用 CGI::Application 的 Perl 应用程序通过 Nginx,它们之间使用 FastCGI 进行通信。 Nginx 不断返回“502 Bad Gateway”,错误
我对 C++ 很陌生,所以很抱歉,如果我问一些愚蠢的问题,但我在网上找不到答案(只有一篇引用 python ( Can mmap and gzip collaborate? ) 的帖子),试图看看是否
我正在试验不同类型的 OpenCV 的 FAST 检测器。 可用的类型有: TYPE_5_8, TYPE_7_12, TYPE_9_16 最后一个是默认的,用这张照片描述: 我假设 TYPE_7_12
我正在尝试开发一个 android 应用程序,它应该分析来自相机的帧并检测角落。 我的目标是检测当前棋盘状态并向服务器提供数据。 我已经在我的应用程序中实现了 OpenCV,并且正在尝试使用 FAST
我正在使用 Tensorflow 和 faster_rcnn_inception_v2_coco 模型训练对象检测器,但在对视频进行分类时遇到了很多误报。 经过一些研究,我发现我需要在训练过程中添加负
我是一名优秀的程序员,十分优秀!