Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:08:41+00:00 2026-06-12T11:08:41+00:00

I am writing an application in Titanium for Android. I have a lot of

  • 0

I am writing an application in Titanium for Android. I have a lot of code in a single JS file. I would like to know if there is any function like php’s include to break the code into multiple files and then just include them.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:08:42+00:00Added an answer on June 12, 2026 at 11:08 am

    Use the CommonJS / RequireJS approach, specifically the require command. This is the (strongly) recommended way for dealing with large systems in Titanium, and is well documented on their website, along with many best practices for dealing with JavaScript modularization specific to Titanium. Here is the documentation from Titanium on this.

    For example, to create a module that encapsulates a ‘view’ of some kind, put it in a file named MyCustomView.js with this content:

    // MyCustomView.js
    function MyCustomView(message) {
        var self = Ti.UI.createView({
            backgroundColor : 'red'
        });
        var label = Ti.UI.createLabel({
            text : message,
            top : 15,
            .... // Other initialization
        });
    
        // ... Other initialization for your custom view, event listeners etc.
        return self;
    }
    
    module.exports = MyCustomView;
    

    Now you can easily use this module in another class, lets assume you put this in your /Resources folder, lets load the module inside app.js and add it to the main window.

    // app.js
    var MyCustomView = require('MyCustomView');
    var myView = new MyCustomView('A message!');
    Titanium.UI.currentWindow.add(myView);
    

    You can use this approach to make custom views, libraries of reusable code, and anything else you would like. Another common thing would be to have a Utility class that has many different helper functions:

    // Utility.js
    exports.cleanString = function(string) {
        // Replace evil characters
        var ret = string.replace(/[|&;$%@"<>()+,]/g, "");
        // replace double single quotes
        return ret.replace(/"/g, "''");
    }
    

    This method can be easily used like this:

    // app.js
    var Utility = require('Utility.js');
    Ti.API.info(Utility.cleanString('He##o W&orld$'));
    

    Another common method I use it for is to implement the Singleton pattern as each module loaded is its own functional context, so if you like, you can have values that persist:

    // ManagerSingleton.js
    var SpriteManager = {
       count : 0
    };
    
    exports.addSprite = function() {
        SpriteManager.count++;
    }
    
    exports.removeSprite = function() {
        SpriteManager.count--;
    }
    

    You would load and use this much the same way as Utility:

    // app.js
    var ManagerSingleton = require('ManagerSingleton');
    ManagerSingleton.addSprite();
    

    This is something of a more elegant solution instead of using global variables. These methods are by no means perfect, but they have saved me a lot of time and frustration, and added depth, elegance, and readability to my Titanium code for Apps of all sizes and types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing an application with command line interface and I would like to know at
Problem Description I am writing application for Android and use native code, And test
Background I'm writing application for android, using Eclipse in Windows. I'm implementing C code
So. Writing application in C, unix, and i have function, which requests user input
I want to start writing application on cloud. Is there any software or free
Writing GWT application. I have CellTable and a code that i'v got from google-example
I´m writing an application for Android in which I have a character that I
I am writing application on ActionScript for Android using Adobe AIR with native extentions.
I am writing an application that will allow an android phone and java application
im writing application on asp mvc. so, i have jQuery tabs on index.aspx, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.