Modules, Top Tabris.js Feature #8

Modules, Top Tabris.js Feature #8

On April 30th Tabris.js 1.0 was released. Tabris.js is a mobile framework which enables developers to write cross-platform, native, mobile applications entirely in JavaScript. To celebrate the release, I’m counting down my Top 10 Tabris.js features.

As soon as a project scales larger than a Hello, World! program, project structure becomes important. Creating boundaries between components, defining APIs and decoupling unrelated pieces all make for a more cleaner, easier to maintain, design. Tabris.js supports this with npm and the Common.js module system.

Number 8 on my list is the Module System.

Tabris.js uses Common.js to define modules. Typically, modules are defined in their own file. A module defines its exports:

  exports.first_name = "John";
  exports.last_name = "Smith";
  exports.age = 38;

And can depend on other modules through the use the require function.

  var person = require("person.js");

For more complicated modules, Tabris.js supports module definitions via a package.json. The package.json is the module definition used by npm (the Node Package Manager). A package.json specifies a modules name, version, metadata such as author and license, and the main entry point for the module.
  "name" : "person",
  "version" : "1.0.0",
  "main" : "person.js",
  "license" : "Eclipse Public License"

Since Tabris.js uses npm’s standard packaging files, depending on node modules is a breeze. For example, if you need the CRC32 algorithm, simply require it in your app and type npm install crc-32. Or, add it as a dependency in your main package.json and type npm install.

Finally, Tabris.js itself has been packaged as a node module and can be installed with NPM. This allows you to target specific Tabris.js versions and track those dependencies in your own modules.

Special thanks to Tim Buschtöns for the work on the module system in Tabris.js. You can read his excellent blog post for more information.

For more information on Tabris.js, follow us on Twitter.