Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked May 25th 2010 by Mattias Svedhem
  • include
  • mobile
0 Comments

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.

    — answered May 25th 2010 by Ryan Gartin
    permalink
    0 Comments
  • 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 :)

    — answered May 26th 2010 by Samuel Lopez
    permalink
    0 Comments
  • 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.

    — answered November 21st 2010 by Bert Grantges
    permalink
    0 Comments
  • 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"

    — answered May 25th 2010 by Mattias Svedhem
    permalink
    0 Comments
  • 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.

    — answered May 25th 2010 by Vinh Bui
    permalink
    0 Comments
  • 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));

    — answered May 25th 2010 by Mattias Svedhem
    permalink
    0 Comments
  • "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

    — answered May 26th 2010 by Stephen Gilboy
    permalink
    0 Comments
  • >> 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

    — answered May 25th 2010 by Stephen Gilboy
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.