Exactly how does Titanium.include work?
In the docs it says:
"one or more filenames to include as if the Javascript code was written in place. This is similar to a C #include function."
I have a mapview in two places with different properties. The eventhandler for clicking on buttons in annotations however, is the same.
Thus I want to include the eventlistener so I don't need to write that code twice.
So I create the mapview and include the eventlistener afterwards, but how do I use the mapview variable in the included file ?
Thanks
8 Answers
-
When Titanium is compiling your project and sees an include js file it will go out and grab the file and copy and paste it (basically) into wherever you made the call.
All variables are accessible…because Titanium sees it as one big file, in order.
-
Mate all you have to do when declaring a variable like the one you are trying to declare:
var mapview = Titanium.Map.createView();
Ti.include("event.js");
is to remove the var, once you do this this variable will be global and immediately after this you include the file like you did, so basically remove the var and you should be fine.
Hope I was of any help to you :)
-
I've noticed an interesting point to this that I thought I'd share.
This is true only if you are including the file using the Titanium.include function - HOWEVER - if you are using another source file through another means (ex. Ti.UI.createWindow(url: 'windows/mynewfile.js')) then all variables within the hosting window will be out of scope for the new window created and thus you will need to re-inculde any files etc.
-
Well, yes that makes sense. However I'm not able to use variables declared in the "main" file.
example:
File: main.js
var mapview = Titanium.Map.createView();
Ti.include("event.js");File: event.js
mapview.addEventListener('click',function(e));
In this case I get an error in event.js saying "Can't find variable: mapview"
-
The javascript is evaluated in the order it appears. So if your include is on the top of main.js and your variable is declare below the include, it will blow up. Swap the location of the variable declare and the include, OR wrap the bits in event.js in a function and call the function when you are ready to execute that code.
-
Hi, thanks for your answer. I do understand that, but I declare the variable first, and then include my event. like this:
The included file is in the directory includes.File: main.js
var mapview = Titanium.Map.createView();
Ti.include("includes/event.js");
File: event.js
mapview.addEventListener('click',function(e));
-
"Well, yes that makes sense. However I'm not able to use variables declared in the "main" file"
i've got app.js including one file where controls are declared, and then in a later included file i am doing stuff to those controls…. spelling, capitalization and order, all good things to check and look for
-
>> In the docs it says: "one or more filenames to include as if the Javascript code was written in place"
so what part of that is open to interpretation ?
if you have
File: main.js
var a = 1;
var b = 2;
Ti.API.info("" + a + " + " + b + " = " (a + b);That is the exact same thing as
File: b.js
var b = 2;
File: main.js
var a = 1;
Ti.include("b.js");
Ti.API.info("" + a + " + " + b + " = " (a + b);The include method just allows you to break up your code into different files…. in the end it all merges into one big file