Titanium Community Questions & Answer Archive

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

No Titanium.include() for Desktop?

Am I missing something, or is there no Titanium.include in the Desktop product? Also, I don't see a shortcut for Ti, so any code I've written before with Ti. instead of Titanium. doesn't work – it just silently fails.

Please get the Desktop and Mobile products in sync, or much of the appeal of Titanium starts to erode.

— asked April 11th 2010 by Mark Burggraf
  • desktop
  • include
  • mobile

3 Answers

  • I guess you can set an alias for Titanium like this

    — answered April 18th 2010 by Stefan Washietl
  • Ti.include is not implemented in desktop SDK? It would be great to have an include statement taht behave like C #include or python import because using DOM to include scripts at runtime must be coded with events in mind (eg: waiting some script to finish loading before using it). Maybe we can just bypass this by reading the script file to include via Ti.FileSystem and eval its content directly into some global namespaced var (its quite a dirty way though).

    Implemented as this (but doesn't work as expected due to eval scope):

    include = function (modules, root_dir, namespaced) {
        var sep = Titanium.Filesystem.getSeparator();
        // sanitize root_dir
        if (typeof(root_dir) == 'string')
            root_dir = Titanium.Filesystem.getFile(root_dir);
        if (!root_dir || !(root_dir.exists && root_dir.exists() && root_dir.isDirectory()))
            root_dir = Titanium.Filesystem.getResourcesDirectory()
        // sanitize modules arg
        if (typeof(modules) == 'string') {
            modules = [modules];
        for (i=0;i<modules.length;i++) {
            if (!(modules[i].length < 4)) {
                // remove js file extension if present
                if (modules[i].match(/^.*(\.js)$/gi)) {
                    modules[i] = modules[i].slice(0, modules[i].length - 3);
                // emulating partial dotted python import
                modules[i].replace(/\./gi, sep);
                // test file reference
                var file = Titanium.Filesystem.getFile(root_dir, modules[i] + '.js');
                console.log('module path:', modules[i], file.exists());
                if (file.exists()) {
                    // read file
                    var content = file.read();
                    // set namespace
                    if (namespaced && namespaced == true) {
                        var ns_hierarchy = modules[i].split(sep);
                        var current_sub = window;
                        for (j=0;j<ns_hierarchy.length;j++) {
                            if (j < (ns_hierarchy.length - 1)) {
                                if (!current_sub[ns_hierarchy[j]])
                                    current_sub[ns_hierarchy[j]] = {};
                            else {
                                current_sub[ns_hierarchy[j]] = eval(content);
                            current_sub = current_sub[ns_hierarchy[j]]
                    else {
                        with (window) eval(content);
    — answered March 7th 2011 by Nicolas Vandamme
  • Any news on this? Surprised that it isn't included in the Desktop version.

    — answered June 25th 2011 by Joe McCann
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.