Datasheet

Chapter 1: Enhancing Development with Dojo Core
was done: Dots in the module names were replaced with slashes, a
.js
was added, and a base URL prefix
was constructed from the location of
dojo.js
.
Most of these modules themselves include further
dojo.require()
statements, which in turn result in
more includes. The thing is, though, many of these
dojo.require()
statements end up asking for the
same things. Luckily,
dojo.require()
has enough smarts to know when something has already been
loaded and won’t try loading it again.
Providing Dependencies with dojo.provide()
Actually, the smarts don’t lie completely within
dojo.require()
. Rather, they depend upon convention
in the use of
dojo.provide()
. Each module in Dojo starts with a call to
dojo.provide()
, which registers
that module as having been loaded. For example,
dijit/form/TextBox.js
begins with the following:
dojo.provide("dijit.form.TextBox");
There’s another benefit to using
dojo.provide()
in your own code, though. Consider whether you’ve
ever seen or written something like the following:
if (typeof window.decafbad == ‘undefined’)
window.decafbad = {};
if (typeof decafbad.util == ‘undefined’)
decafbad.util = {};
if (typeof decafbad.util.foo == ‘undefined’)
decafbad.util.foo = {};
decafbad.util.foo.aMethod = function() {
// method body
}
This is the convention by which namespaces are established in many modern JavaScript applications,
ensuring the existence of each part of the namespace before trying to use it. Sometimes only the last part
of the namespace is created because all the parent namespaces have been created in other dependencies
loaded earlier, but this is the general form of the dance.
This can be replaced with a more concise and descriptive call to
dojo.provide()
, like so:
dojo.provide("decafbad.util.foo")
decafbad.util.foo.aMethod = function() {
// method body
}
Not only does this call register the namespace as loaded with respect to
dojo.require()
,italsoensures
that the full namespace itself is created as necessary. This provides both an implementation and a read-
ability benefit for your code.
Telling Dojo Where to Find Your Modules
There’s one more piece to the dependencies story: How does Dojo find your code? By default,
dojo.require()
attempts to load all modules from the parent URL where
dojo.js
was found. This
9