Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5976813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:16:53+00:00 2026-05-22T21:16:53+00:00

This should be a very simple concept, but I just don’t understand. In a

  • 0

This should be a very simple concept, but I just don’t understand. In a Titanium app I have an array of data used by several windows, my xhr result needs to be passed to the top level of my app’s namespace to be added to the array. I can successfully parse the JSON response inside of the onload function, but I want to separate my data code from my UI generation.

Here is a simplified app.js version so that I might be able to understand the concept. And no, I won’t pollute the global namespace in my real app.

Titanium.UI.setBackgroundColor('#000');
var myArray = [];
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
    myArray = JSON.parse(this.responseText);
    // var data = JSON.parse(this.responseText); // no help
    // myArray.push(data); // no help
    // return myArray; // no help
};
xhr.onerror = function() {
    Titanium.UI.createAlertDialog({ message:"Something has gone terrible wrong."});
};
xhr.open('GET','http://myapp.com/data.json');
xhr.send();
var win = Ti.UI.createWindow();
var view = Titanium.UI.createView({
    backgroundColor:'green'
});
var caption = myArray[2].caption;
var label = Ti.UI.createLabel({
    color:'white',
    text:caption,
    textAlign:'center'
});
view.add(label); 
win.add(view);
win.open();

Thanks for your patience!

Edit

This produces the the correct result from the user’s perspective, but I want to access the array outside the scope of the onload function. I don’t want to have UI code mixed with API calls.

xhr.onload = function() {
    myArray = JSON.parse(this.responseText);
    var caption = myArray[2].caption;
    var label = Ti.UI.createLabel({
        color:'white',
        text:caption,
        textAlign:'center'
    });
    view.add(label); 
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:16:54+00:00Added an answer on May 22, 2026 at 9:16 pm

    The code is being run asynchronously. The label is attempting to generate before the xhr.onload has begun.

    you should fire an event from the onload method of your code.

    the event will have a listener in the UI section of your application and it will provide the appropriate separation of http code from the ui code; Something like this

    xhr.onload = function() {
        myArray = Ti.App.fireEvent("app.updateLabel",
                          {"responseText":this.responseText});
    };
    

    in your UI, view code

    Ti.App.addEventListener("app.updateLabel",function(data) {
        myArray = JSON.parse(data.responseText);
        var caption = myArray[2].caption;
        var label = Ti.UI.createLabel({
            color:'white',
            text:caption,
            textAlign:'center'
        });
        view.add(label); 
    });
    

    more on events from appcelerator documentation

    and I have some examples on my blog, http://blog.clearlyinnovative.com, also

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be very simple, but its not working! I have this link on
I feel as if this should be very simple, but it's behaving strangely. I
This should be a very simple case, but I am pulling hair trying to
This should be very simple, but I cannot find the correct syntax using search.
This should be very simple. I have a function f(x) , and I want
This should be very simple but I could not find an exhaustive answer: I
This should be a very simple problem you would think. I have a box
This should be very simple, but I'm blocking on this.... Let's say, for the
This should be very simple for someone who knows regex. I don't need anything
This should be very simple question. There are many programming languages out there, compiled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.