Understanding Appcelerator Ti.include & require

prashant picture prashant · Feb 6, 2012 · Viewed 8.2k times · Source

I've started looking into Appcelerator 1.8.0 few days and back and i'm a little confused with Ti.include and require. As i understand, Ti.include is used to include content of a js file in another file, and require is used to get the public api exposed using exports. What i'm not able to understand is if require-export can be used to get hold of public API's do you really need include? Basically i'm trying to understand different between include and require?

Answer

Jeff Bonnes picture Jeff Bonnes · Feb 9, 2012

Ti.require uses the CommonJS specification. Although files accessed via Ti.require have access to the Ti namespace, they do not have access to the Global namespace - any variables or functions you have declared in the main program. Ti.include files do have access to the global space and can modify or add to it. Ti.require is preferred, but not always practical. See https://wiki.appcelerator.org/display/guides/CommonJS+Modules+in+Titanium for information on the Ti.require function and https://wiki.appcelerator.org/display/guides/Mobile+Best+Practices for more best practices.