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.
3 Answers
-
I guess you can set an alias for Titanium like this
Ti=Titanium;
-
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]; } console.log(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); } } } } }
-
Any news on this? Surprised that it isn't included in the Desktop version.