开发者

Accessing variable outside on load function

Hi I am working Android application development using titanium studio.I have developed small application.my problem is that I can not access variable which is define inside the xhr.on load.I used following code:

xhr.onload = function()
{
var json = this.responseText;
var to_array = JSON.parse(json);
var to_count = to_array.length;
};

I want to access to_count and to_array outside onload function and pass it to another child window.For that开发者_开发问答 I used following code:

var feedWin = Titanium.UI.createWindow({
    url:'home/feed.js'
});//alert(to_count);
feedwin.to_array = to_array;
feedwin.to_count = to_count; 


The XHR client is asychronous by default, which means that code will continue to execute while the XHR is running. If you have code that is dependent on your XHR being finished, then you will need to either call that code from within the onload function, or force the XHR to be synchronous by adding "false" as a third parameter to xhr.send() (I've found the first option to be the more reliable one, and more in line with what Titanium expects/feels is best practice, just FYI).


The best way to accomplish this is to initialize your feedWin in the onload. So, one of the following two snippets should work:

xhr.onload = function()
{
    var json = this.responseText,
        feedWin = Titanium.UI.createWindow({
            url:'home/feed.js'
        });//alert(to_count);

    feedWin.to_array = JSON.parse(json);
    feedWinto_count = to_array.length;
};

or

var feedWin = Titanium.UI.createWindow({
        url:'home/feed.js'
    });

xhr.onload = function()
{
    var json = this.responseText,

    feedWin.to_array = JSON.parse(json);
    feedWinto_count = to_array.length;
};

I'm not familiar with Titanium, so I don't know particulars, but that is my best guess.


I am not very familiar with Titanium, but wrt to scope of declaration, I think this is what you need to do to use them outside the function.

var to_array;
var to_count;


xhr.onload = function()
{
var json = this.responseText;
to_array = JSON.parse(json);
to_count = to_array.length;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜