For a Titanium app that I’m writing, I’ve structured a modular approach to where other developers (when I’ve moved on from my organisation or others have been asked to add onto the app) can add new modules to the app easily (not to be confused with native code modules).
I’m having difficulty figuring out how to load a module from an array and call a common central bootstrap-type method to initialise the module. The app uses the namespace approach as suggested by the authors of Tweetanium.
The app uses the namespace: org.app
I’ve defined an array that contains the configured modules that are to be loaded in the app:
org.modules = [
{
name: 'messages',
enabled: true,
title: 'Messages'
},
{
name: 'calendar',
enabled: true,
title: 'Calendar'
}
];
Each module has the namespace: org.module.moduleName where moduleName is the name of the module from the array (e.g. messages or calendar).
I have created a modules directory and I have been successful in dynamically including the js file for each module that is enabled (tested by specifically statically calling the method). I need to call the createModuleWindow() method from the module code to obtain the main view for that module.
org.module = {};
org.module.moduleManager = [];
// Loop through the list of modules and include the
// main modulename.js file if enabled.
// Create the module and add it to the module manager array
var modules = org.modules;
var config = org.config;
for (var i = 0; i < modules.length; i++) {
var _module = modules[i];
if (_module.enabled) {
Ti.include(config.moduleBasePath + _module.name + "/" + _module.name + '.js');
org.module.moduleManager.push(createModuleWindow(_module.name));
}
}
function createModuleWindow(moduleName) {
// Not sure what to do here.
return org.module.[moduleName].createMainWindow();
};
For createModuleWindow(), I’ve tried dynamic classes and the square bracket notation but I just get errors like ‘moduleName’ isn’t a constructor (in the case of using a class approach) or a parse error in the case of the above code.
How can I dynamically call the namespaced module method?
Your syntax error is here:
There should be no dot:
A couple other notes:
org.moduleis empty in your example code, so the line above will throw a null-pointer exception. (You’re probably just not showing all your code.)Use of both
org.module(singular) andorg.modules(plural) is needlessly confusing. I would try to better differentiate the two in the code.