Requirejs Introduction
Requirejs is a JavaScript module loader. It is ideal for use in browsers. Loading a modular script with Requirejs will increase the load speed and quality of your code.
compatibility
Browser (browser)
|
is compatible |
| IE 6+ |
Compatible? |
| Firefox |
Compatible? |
| Safari 3.2+ |
Compatible? |
| Chrome |
Compatible? |
| Opera |
Compatible? |
Advantages
Realize the asynchronous loading of JS file, avoid the dependency between the Web page loss Response Management module, and facilitate the writing and maintenance of the code.
Get started quickly
- Step 1
- Introduction of Require.js
- The dependency in require () is an array, even if there is only one dependency, you must also use an array to define
- The second parameter is a callback function (callback) that can be used to resolve dependencies between modules
<! DOCTYPE html>
- Step 2
- Require.config is used to configure the module load location
- If the fixed position is longer, you can use BASEURL: "JS", then the paths will not have to write JS
<! DOCTYPE html>
- Step 3
- The Require.config configuration is repeated in step 2, which is not good if each page is added to the configuration, and Requirejs provides a function called "Master data".
- Create a main.js put the Step 2 require.config in the Main.js
<script data-main= "Js/main" src= "Js/require.js" ></script>
- Step 4
- Modules loaded through require are generally required to comply with the AMD specification, using define to declare the module, but in some cases need to load non-AMD specification JS, this time need to use another function: Shim
Require.config ({ shim: { "underscore": { exports: "_"; }, "Jquery.form": ["jquery"] }}); Require (["jquery", "jquery.form"], function ($) { $ (function () { $ ("#form"). Ajaxsubmit ({...}) ;});
links
Nanyi about JS Modular programming Instructions:
- Http://www.ruanyifeng.com/blog/2012/10/javascript_module.html
- Http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.html
- Http://www.ruanyifeng.com/blog/2012/11/require_js.html
Requirejs Simple introduction that uses