javascriptiostitaniumtitanium-mobilecommonjs

What is the correct CommonJS structure for a Titanium project?


I'm starting a new Titanium app and want to use best practices and avoid having memory leaks from the get go. I'm new to CommonJS as well as the Titanium platform in general.

Unfortunately it seems that all the sample applications for titanium surround on Ti.include("/lib/module") instead of the newer recommended best practice of require("/lib/module").

What I am worried about is the memory consumption of using CommonJS could require. From the CommonJS Modules in Titanium documentation it states that modules will be cached, wouldn't this mean that if I ever access a module that all those functions suddenly stay around in memory even if they go out of scope?

I've started a new app with the following structure

/ctrl           # Model/UI controllers
/lib            # libraries (common + 3rd party)
/ui             # UI forms
/model          # DAL objects for data store

From here my main app has a single dashboard style view which is loosely structured as follows:

(function() {
    var getMenuItem = require("/ui/main").getMenuItem;
    var win = Titanium.UI.createWindow({
        title:'Main',
        backgroundColor:'#fff'
    });
    var nav = Ti.UI.iPhone.createNavigationGroup({
        window:win
    });
    var sect;
    var data = [];
    sect = Ti.UI.createTableViewSection();
    data.push(sect);
    sect.add(getMenuItem("Customers",
        require("/ctrl/account").createCustMainWindow));
    sect.add(getMenuItem("Schedules",
        require("/ctrl/schedule").createScheduleMainWindow));
    sect.add(getMenuItem("Settings"));
    var menu = Titanium.UI.createTableView({
        style: Ti.UI.iPhone.TableViewStyle.GROUPED,
        data:data
    });
    win.add(menu);
    menu.addEventListener('click',function(e) {
        if (e.rowData.createWindow) {
            var win = e.rowData.createWindow(nav);
            nav.open(win);
        }
    });
    var navWindow = Titanium.UI.createWindow();
    navWindow.add(nav);
    navWindow.open();
})();

Any guidance on a correct project structure is greatly appreciated.


Solution

  • This is the community app under development that purely uses module pattern, also look through the developer blog to find Forging series which have samples developed mostly using module pattern