Dojo learning notes (1. modules and packages)
Intro:
Dojo is a very powerful object-oriented JavaScript toolbox. It is recommended that you add how to use oo for programming in Javascript, which will be of great use for you to read the dojo source later.
Download dojo 0.3.1. The following descriptions are applicable to this version.
Translation from http://manual.dojotoolkit.org/WikiHome/DojoDotBook/BookUsingDojo
Getting started
1: Add dojo to our webProgramMedium
1. Flag
< Script Type = " Text/JavaScript " >
Djconfig = {Isdebug:False} ;
</ Script >
Djconfig is a global object in Dojo. It provides various options for dojo. isdebug is one of the most common attributes. It is set to true so that debugging output can be seen directly on the page, of course, some of these attributes are related to debugging, so I won't be able to perform this here.
2. reference the startup of dojoCode
< Script Type = " Text/JavaScript " SRC = " /Yourpath/dojo. js " />
In this way, you can reference the code of dojo and directly use some of the commonly used objects to download the downloaded dojo. JS is the code after the compression (remove comments and space). If you want to read it, it is recommended to read dojo. JS. uncompressed. JS, dojo. javascript has about 127 KB, but 211 KB before compression. Why is it so big? It has already integrated some common modules into dojo. JS, so it looks a little bigger. build.txt shows the default dojo. which modules does JS contain?
3. Declare the package you want
< Script Type = " Text/JavaScript " >
Dojo. Require ( " Dojo. Math " );
Dojo. Require ( " Dojo. Io .* " );
Dojo. Require ( " Dojo. widget .* " );
</ Script >
You can think of the Code as a Java import statement or a using statement in C #. If you do not require the code, the module itself is not integrated in Dojo. in JS, there will be a script error.
2. Pre-integrated packages for different needs
Dojo itself is composed of many modules. However, due to the diversity of user requirements, Dojo provides different versions for different needs, you can select many versions when downloading dojo, such as Ajax and Widget. The most important difference between each version is that dojo. JS file, but in addition, each version is fully functional, dojo. JS is integrated into different modules based on different versions.
3. Get the latest dojoSource code
First, you must install the subversion. After the Subversion works properly on your computer, you can download the source code of dojo using the following command:
SVN Co http: // Svn.dojotoolkit.org / Dojo / Trunk /
This will create a trunk directory under your current directory. If you want to directly get to the current directory, use this command:
SVN Co http: // Svn.dojotoolkit.org / Dojo / Trunk / .
Or you want to get to the mydir directory under the current directory, use this command:
SVN Co http: // Svn.dojotoolkit.org / Dojo / Trunk / Mydir
Modules and packages
Module
Dojo code is divided into logic units called modules, which is somewhat similar to the package in Java. In addition to the dojo module, it can contain classes (similar to the classes in Java) and simple functions.
For example, the module "dojo.html" contains a series of functions, such as dojo.html. getcontentbox (), and the module "dojo. DND" contains a series of htmldragobject classes.
Note the naming conventions. The first letter of a function is a lowercase letter, and the first letter of a class is an uppercase letter.
A module can also be called a namespace"
Package
In most cases, the dojo module only needs to be defined in one file, but sometimes, a module may be divided into multiple files, such as javasdojo.html, it was originally defined in a file, but due to the enhancement of the function, the file gradually becomes larger and we have to split it into multiple files, which is mainly for performance consideration, so that the browser can download only the code it needs. Unfortunately, its implementation details are not so transparent to the users of dojo, you must know which file you want to use the function, and then require and use it.
Every such file is called a package.
Dojo. Require ( " Dojo.html. Extras " )
The file src/html/extras. js will be referenced, which will define several (not all) Functions of the dojo.html Module
As far as I know, although a single file can define multiple classes in a package, a single script file cannot define multiple modules (Java can be equivalent to defining two classes in a file ), in addition, the package name and module name can be different. For example, the package dojo.widget.buttondefines dojo.widget.html. button
Basically, you should think that, despite being closely related to modules, packages and modules are two completely different entities.
Why is there a concept like module and package?
Why is there a concept like module and package? To meet your application's need to load what it uses and make full use of the advantages of modular design, Dojo maintains the smallest footprint so that it can still provide the functions you need, why do you need your users to waste time downloading unused JavaScript? When a package is a JS file, a module is essentially a namespace, for example, dojo. style or dojo.html. extras
In most cases, a package contains a module, but more often, a module may be split into several package files.
With packages and modules, you will be able to deliver the most relevant functional code, minimizing code expansion and eliminating the resulting poor user experience, this is the main purpose of the module design. Through modularization, you can introduce custom modules (your own JavaScript tools), and the maintenance module will not have any impact on the core code library.
In addition, the module system of dojo also provides a built-in mechanism to provide namespaces using code. For example, the Event System of dojo defined by the module dojo. Event
How to reference
Set Reference statements
How do you know which package to reference to dojo. Require ()?
1. Module
First, determine the modules you want to use. In this example, we assume that you want to use dojo.lfx.html
2. Package
After the search code, you will find that dojo.lfx.html is defined in two files:
- Src/lfx/html. js
- Src/lfx/extras. js
You can
Dojo. Require ( " Dojo.lfx.html " );
Or
Dojo. Require ( " Dojo.lfx.html " );
Dojo. Require ( " Dojo. lfx. Extras " );
Wildcard
New users may. lfx. * In this way, we are surprised to replace the above two sentences. In fact, _ package __. javascript has defined the statements that can be replaced by wildcards. In this way, Dojo can decide to load specific modules according to the current environment.
To be continued...