Login Register

What dojo.require Does

The modules bundled with Dojo correspond roughly to .js files underneath the Dojo root. Looking underneath your dojo root directory, you will see at least three top-level directories dojo. dijit. and dojox. Everything that you've dojo.require'd so far has begun with one of these three prefixes.

Like most modern languages, Dojo uses "." to separate modules from submodules from sub-submodules, etc. These correspond to directories, subdirectories and sub-subdirectories underneath the Dojo root. The name after the last period is the JavaScript file name. So for example:

dojo.require("dijit.form.Button");
// Esentially loads the script dijit/form/Button.js underneath the Dojo root

Why not just load the scripts with a SCRIPT tag? Well, besides being shorter, a dojo.require statement ensures that modules are not loaded twice. They also make the build system function better, which you'll see in The Build System chapter. You can, however, pull in any Dojo code after the initial dojo.js script tag with a script tag and the package system will ensure that dependencies for that package are still satisfied. This trick is particularly handy when you need line-number debugging information from some new chunk of code you're working on.

The script dijit/form/Button.js in turn starts like this:

dojo.provide("dijit.form.Button");
dojo.require("dijit.form._FormWidget");
dojo.require("dijit._Container");
dojo.declare("dijit.form.Button", dijit.form._FormWidget, {
   // [snip]
}
dojo.declare("dijit.form.DropDownButton", [dijit.form.Button, dijit._Container], {
   // [snip]

All of the classes defined in this script are then available to you. By convention, most modules define one class named after the module itself, as in our dijit.form.Button example. You can also use dijit.form.DropDownButton, if you dojo.require("dijit.form.Button"). But dojo.require("dijit.form.DropDownButton") will not work. Each script provides only one package.

Note, however, that it's Dojo convention to prefix "suggested private" classes with a "_". You shouldn't use these classes, even though JavaScript won't throw an error if you do.