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" });
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.
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 filesI 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.
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' });
Your Answer
Think you can help? Login to answer this question!