1
votes

I've discovered that we can create CommonJS modules instead of native or web ones (take a look at your Titanium folder and search the ti.cloud module). But there is no documentation I can find that explains it...

So, I tried to add my own CommonJS module inside the Titanium folder "commonjs" and imported it to my application using "require". It runs like a charm.

Now, I want to add some other files to my CommonJS module and make modules referencing each other inside the module.

For example in my app I have:

  • main.js
  • ui
    • CommonView.js

In this structure, main.js is my main module and has a method createCommonView() that tries to require the module CommonView (require('./ui/CommonView')) but the compiler complains that the module is not found.

How do I create a CommonJS module with multiple modules that reference each other?

To be precise, I don't want to copy these modules inside my project. I want to keep them in the Titanium folder so it is available to all projects.

1

1 Answers

1
votes

Here's a blog post from Appcelerator talking about CommonJS modules: http://developer.appcelerator.com/blog/2011/12/commonjs-module-guide-for-1-8.html

And here's the guide / documentation from Appcelerator: https://wiki.appcelerator.org/display/guides/CommonJS+Modules+in+Titanium

Right now, all module references are relative to the application root:

  • in app.js, require('main');
  • in main.js, require('ui/CommonView')
  • in CommonView.js, require('ui/SpecificView')

And packaged CommonJS modules (such as ti.cloud, installed in the "modules" directory of your application or Application Support) only support a single JavaScript file.

Hint: I found these guides by just typing "Appcelerator CommonJS" in to Google. I'm not sure what method you used to find the documentation. "require" is also documented extensively in the "Global" object in the Titanium Mobile documentation: http://docs.appcelerator.com/titanium/2.1/index.html#!/api/Global