This paper is a summary of some knowledge points of Requirejs, and is matched with example analysis in multiple pages application.
The directory structure of this case is as follows:
Three main functions of the Requirejs API: Define (Create Module), require (load module), config (configuration)
1, the HTML file loaded JS file
Page1.html contents are as follows:
Page2.html contents are as follows:
Knowledge Extension:
The Data-main property specifies the main module of the Web page program, which is Requirejs first loaded. Because requirejs default file suffix name is js, so you can page1.js abbreviated to Page1
Root path precedence rules for loading script files
When the module is loaded with require (), the. js suffix is omitted, and it is looked up from the BaseURL, if it has a. js suffix, or starts with "/", or contains a URL protocol (HTTP/HTTPS), the root
According to your specific path settings to find
Config > Data-main > Default BaseURL
Do not set Data-main and config, the default baseurl is the directory of the HTML page that references Require.js
Set Data-main, BaseURL is the directory where the main module is located (for example, the primary module in the first segment of HTML is page1.js, so the root directory is/scripts in its directory)
Configure config, explicitly set BaseURL, or set the path for each module individually
2, the module of the path, etc. to configure
Using the Require.config () method, you can customize the load behavior of a module. In a multiple-page application, you can write the configuration in a common file, such as the Common.js file in this example, and then load the respective modules in the callback function after each page loads the current configuration.
The Common.js code is as follows:
Require.config ({
baseurl: ' Scripts/app ',
paths: {
jquery: [
' http://lib.sinaapp.com/js/jquery/ 1.9.1/jquery-1.9.1.min ',
'. /lib/jquery '
]
}
});
Knowledge Extension:
Supported Configuration items:
BaseURL:
The lookup root path for all modules. Note: When the loaded JS file (at the end of. JS, with the "/" beginning, containing the agreement), will not use BaseURL;
Paths:
Path maps Those module names that are not placed directly under BaseURL. The starting position when you set path is relative to BaseURL, unless the path setting starts with "/" or contains a URL protocol;
Note: The path defined in paths cannot contain the. js suffix, because the path resolution mechanism automatically adds the. js suffix, and the load path can be set multiple, such as failure from the CDN load, loading the local JS file;
Shim
Configure modules that do not use define () to declare dependencies;
There are two parameters to note:
(1) Exports value (output variable name), Exposure method interface
(2) Deps array, indicating the dependence of the module
Such as:
Require.config ({
baseurl: '/scripts/lib ',
shim:{
zepto: {
exports: ' $ '
},
backbone: {
Deps: [' underscore ', ' zepto '],
exports: ' Backbone '
},
' zepto.animate ': [' Zepto ']
}
});
3, the module loading
The Page1.js code is as follows:
Require (['./common '], function (common) {
require ([' sayPage1 '], function (sayPage1) {
Saypage1.hello ();
}) ;
});
The Page2.js code is as follows:
Require (['./common '], function (common) {
require ([' sayPage2 '], function (sayPage2) {
Saypage2.hello ();
}) ;
});
Knowledge Development:
The Require () function accepts two parameters. The first parameter is an array that represents the dependent module; The second parameter is a callback function that is invoked when the module specified by the current plane has been successfully loaded. The loaded module can be invoked as a parameter to the callback function.
Here in order to ensure that the configuration is completed before loading the required modules, mainly for the correct path resolution, in the callback function to require.
4, the definition of the module
Code in Saypage1.js:
define ([' jquery '], function ($) {
function sayhi () {
$ (' body '). Append ('
Knowledge Extension:
The Define function also accepts two parameters. The first argument is an array of dependent modules, and the second parameter is a callback function.
Of course, the last on-line when the JS file to merge and compression, you can use R.js, convenient and quick ~
The above is a small set to introduce Requirejs multiple page application analysis, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!