Login Register

Developing a Module

All the magic that is the dojo module system takes place in the src directory. Take a look in your distribution at the various files an directories. You'll notice that there are many directories and many js files. Now look inside the directories. Most of the directories have a file named __package__.js along with many other js files.

There are several ways to develop a module. Basically, it all has to do with how you plan to divide up your code. If you're writing a small module which needs the full source code to work, you'll want to put a js file in the base of the src directory with the same name as your module and a .js on the end. This can be called by putting the following in your source code:

dojo.require("dojo.packagename");

If you want to split your module into several unrelated files (called "resources"), you'd create a directory an put each part into a seperate js file. The only way you could call this is by using a deeper require statement, as follows:

dojo.require("dojo.packagename.subpackage");

If you want to split your module into smaller pieces that sometimes rely on each other, then you'll want a __package__.js file. What this file does is specifies default resources to load depending on the environment. Open up some of your files and see how it looks. There are two important functions: kwCompoundRequire and provide.ÂÂ

The first function specified which packages to load in an object with keys consisting of the different environments. An example is as follows:

dojo.kwCompoundRequire({

common: ["dojo.package.first", "dojo.package.second"],

browser: ["dojo.package.browser"]

});

The second function tells dojo that your module was loaded properly.

dojo.provide("dojo.package.*");

In all the other js files, you'll need to have a require statement to inform dojo that the package pieces have been loaded. This is also true of the files in the base of the src directory:

dojo.provide("dojo.package");