YUI module development principles _ basic knowledge-js tutorial

Source: Internet
Author: User
This article mainly introduces the principle of YUI modular development and provides code for you to learn and use. As Internet applications become heavier and heavier, JavaScript code becomes increasingly large. How to effectively organize your own code, it becomes very important. We should learn to control our code, instead of having no idea where the last bunch of bugs come from. Front-end modular development can help us effectively manage Code, especially when many people develop, to improve development efficiency.

The YUI generation module is:
YUI. add ('lele1', function (Y) {...}, '1. 0.0 ', requires: ['lele2']);
YUI is a global variable, similar to $ in jquery. In the add method, the first parameter is the module name module1, the second parameter is an anonymous function, which contains the module content, and the third parameter is the version name, the fourth requires indicates the module dependency. The preceding figure shows that module1 depends on module2 (that is, module2 must be executed before module1 ).
Normally, each module is stored in a js file named after the module name. That is, module module1 is stored in the module1.js file, and module 2 is stored in the mudule2.js file.
Module module1:
// Load the YUI seed file, including all dependencies of YUI

The Code is as follows:


Script
Script
YUI (). use ('lele1', function (Y ){...});
Script

Next we will analyze what will happen in the above line of code.
1) YUI first analyzes the dependency between the module1 module and creates a URL: http: // localhost: 3000/yui/combo? Mudule2.js & module1.js. Note that module2.js is in front of modul1.js.
2) create a dynamic script tag and request js files from the server.

The Code is as follows:


Var script = document. createElement ('script ');
Script = 'HTTP: // localhost: 3000/yui/combo? Mudule2.js & module1.js ';
If (script. readyState ){
// IE
Script. onreadystatechange = function (){
If (script. readyState = "loaded" | script. readyState = "complete "){
Script. onreadystatechange = null;
// Reserved
}
};
} Else {
// Non-IE
Script. onload = function (){
// Reserved
};
}
Document. body. append (script );

3) The server detects requests sent from the client, parses the URL, and then searches for the module2.js and module1.js js files. Then, the two files are assembled into one file in order and returned to the client. The contents of the final js file returned are as follows:
  

The Code is as follows:


// Contents in module2.js
YUI. add ('lele2', function (Y) {Y. module2 = {}; Y. module2.name = 'lele2';}, '1. 0.0 ', requires: []);
// Contents in module1.js
YUI. add ('lele1', function (Y) {Y. module1 = {}; Y. module1.name = 'lele1';}, '1. 0.0 ', requires: ['lele2']);

4) The client receives the returned js and starts parsing. That is, it executes the add method in the YUI. the execution process is roughly as follows:

The Code is as follows:


YUI. modules = {};
// Module2
YUI. modules. push (function (Y) {Y. module2 = {}; Y. module2.name = 'lele2 ';});
// Module1
YUI. modules. push (function (Y) {Y. module1 ={}; Y. module1.name = 'lele1 ';});

5) after the resolution is complete, the onload method in step 2 is automatically triggered (onreadystatechange method in IE). The code for "Reserved" in step 2 is as follows:

The Code is as follows:


For (var I = 0, len = YUI. modules. length; I <len; I ++ ){
// Register the api that requires export output in the module under this; this is an instance of YUI, this = new YUI ();
YUI. modules [I] (this );
}
// Callback is the callback function in YUI (). use.
// At this time, the module is parsed and the this parameter is passed in. In callback, you can call the APIS output in module1 and module2 at will.
Callback (this );

The above is a simple introduction to modular development with the help of YUI. The actual process of YUI is much more complicated than above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.