Can't access global var Ti.from included function?

You must Login before you can answer or comment on any questions.

I have the following code:

In file1.js:

var steps = ['on', 'off'];
Ti.include('included_file.js');
In included_file.js:
var imgStep1 = Ti.UI.createImageView({
    image : "step_1_"+steps[0]+".png"
});
I am confused as to why the steps array is not accessible from the createImageView function?

4 Answers

Hi Torimoto-san,

Could you try including file1.js inside included_file.js?

Eg -

Ti.include('file1.js');
var imgStep1 = Ti.UI.createImageView({
    image : "step_1_"+steps[0]+".png"
});

— answered 11 months ago by Shebin Rawther
answer permalink
1 Comment
  • Hrm - I will try that later but what I need is the ability to give a commonly included file different variables to give it the ability to behave differently based on that variable. In that case, what are my alternatives? Using Ti.App.Properties just to send over a variable to an included file seems like overkill - I thought my original code should work...?

    — commented 11 months ago by Takahito Torimoto

This code should work as expected. Make Sure there is no other Point of failure. However I suggest you to put your vars/functions in namespaces and make a sort of registry/config. e.g.

var app = {};
app.config = {};
app.config.var = value;
include them once at the very beginning. then you dann Access them in your whole app. or use the commonKS pattern.

— answered 11 months ago by Alexander Bauer
answer permalink
2 Comments
  • commonJS.. these smartphones always think they're better..

    — commented 11 months ago by Alexander Bauer

  • I thought about doing the above, but I thought the way I was trying to do it was simpler and made more sense (as opposed to having a namespace/registry tree for something that I only needed to use once). You'll see further below that I figured out it had to do with creating a Window and not having the variable (to be accessed by the Ti.include() file) defined at the root app.js - which seems odd. I would have thought that I could define it at the Window level. If this is normal behavior, then the namespace/registry tree method would also have to be defined in the root app.js - which won't work with the way I have my app structured... this just seems odd.

    — commented 11 months ago by Takahito Torimoto

I have yet to understand how commonJS works as I followed the videos/tutorials and Android always give me fits using commonJS. I probably don't understand it well enough.

I use the namespace method as shown above by Alexander. I have a variety of varibales,functions,etc that need to be accessed in several parts of my applications. So I use the following method:

var app = {}; //My app namespace as not to pollute any globals.
app.someFunction = function(args){
    //FUNCTION
} //A global type function that I may need to use many places
app.varName = 'some value'; //A global type variable that I may need to access from various functions or js files
I usually have a few sub-name spaces as well like app.ajax, app.events, app.ui, etc to help organize things. It seems to work pretty good for me. I imagine it may not be as efficient as CommonJS but for smaller apps, it seems to work well.

— answered 11 months ago by Lee Bartelme
answer permalink
1 Comment
  • My only takeaway so far about CommonJS is using require to include modules where exports are used to define reusable methods and parameters - similar to the namespace examples you gave (although prototypes are almost always appropriate for methods, and even sometimes for Class-level-like shared parameters).

    — commented 11 months ago by Takahito Torimoto

Are both files in the same directory (to make sure that included_file.js is actually included)?

I tried this out with the following code:

In app.js:

var win = Ti.UI.createWindow({
    backgroundColor:'white'
});
 
var steps = ['on', 'off'];
Ti.include('test.js');
 
win.open();
And in test.js:
alert(steps[0]);
An alert (with 'on' as the message) is displayed when the above code is executed, which means that the steps array is accessible from within the included test.js file.

Could you try something similar (alerting steps[0] in included_file.js)? If it does work, then maybe you could try:

var tempStep = steps[0];
 
var imgStep1 = Ti.UI.createImageView({
    image : 'step_1_'+tempStep+'.png'
});

— answered 11 months ago by Pallavi Sivakumaran
answer permalink
5 Comments
  • ok your examples all worked, and I narrowed it down to something specific.
    Here's the difference between your code (which works) and mine (that doesn't).

    app.js:

    var win = Ti.UI.createWindow({
        url:                "app2.js",      
    });
     
    win.open();
    app2.js:
    var steps = ['on', 'off'];
    Ti.include('test.js');
    test.js:
    alert(steps[0]);
    This fails. But if I were to move the steps definition to app.js, it works. i.e. if I placed the following code at the top of app.js (not app2.js):
    var steps = ['off', 'on']; // note the reversal of values just to validate the difference
    Then the alert shows "off".

    So - I thought Windows became their own root context? Or are we saying that any Ti.include() files have access to variables only defined in the app.js-level root context - which I thought would be the created Window, and not all the way back to the root app.js?

    — commented 11 months ago by Takahito Torimoto

  • Hrmm... is this a bug? I just saw in the 2.1 Documentation (I'm using SDK 2.1.0) the following:

    "The included JavaScript is interpreted in the current root execution context -- that is, either app.js or in the root context of a window opened with the url property."

    So I think my assumption was right in that - in my example above, I should be able to define the variable (at the global-level) within app2.js and access it from the included file.

    — commented 11 months ago by Takahito Torimoto

  • probably a Bug, didnt Test it. however I dont think anybody will give attention to that since using url is type of Bad practise and being propagated since years by appc.

    — commented 11 months ago by Alexander Bauer

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!