Managing Source Code with Modules
If you've programmed for any amount of time, you've been exposed
to the concept of grouping related pieces of code into related blocks,
whether they be called libraries, packages, or modules, and pulling in
these resources when you need them via a mechanism like an import statement or a #include preprocessor directive. Dojo's
official means of accomplishing the same kind of concept is via
dojo.provide and dojo.require, respectively.
In Dojo parlance, reusable chunks of code are called
resources and collections of related resources
are grouped into what are known as modules. Base
provides two incredibly simple constructs for importing modules and
resources: dojo.require and
dojo.provide. In short, you include
a dojo.provide statement as the
first line of a file that you want to make available for a dojo.require statement to pull into a page.
As it turns out, dojo.require is a
lot more than just a placeholder like a SCRIPT tag; it takes care of mapping a
module to a particular location on disk, fetching the code, and
caching modules and resources that have previously been dojo.require d. Given that each dojo.require statement incurs at least one
round trip call to the server if the resource is not already loaded,
the caching can turn out to be a tremendous optimization; even the
caching that you gain from requiring a resource one time and ensuring
it is available locally from that point forward is a great
optimization.
Motivation for Managing the Mayhem ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access