Getting started with my module Loading System v17 2

Source: Internet
Author: User

In section 1, we talked about using "./" relative to the current directory, "./" relative to the parent directory, and "http://www.cnblogs.com/#" relative to the parent directory. If the remote file directly uses url. In fact, another type of module identity is relative to the root directory, which is specifically the directory where mass. JS is located. Of course, you can modify the root directory by means of configuration, but it is not recommended to do so. For example, AAA. js and Mass. js are stored in the same directory:

 
<! Doctype HTML>  

Alias Mechanism

For example, for a project, page a references n JS files, and page B References n JS files. When we use $. require may be very troublesome to call them ". /",".. /"to locate the target JS file. At this time, we may use a special JS file to alias these modules for convenient reference.

For example, we mentioned AAA. JS, BBB. JS, CCC. JS, and DDD. js in section 1. We use a modules to save their aliases.

 
// $. Core. base is mass. JS directory $. config ({alias: {$ AAA: $. core. base + "AAA. JS ", $ BBB: $. core. base + "BBB. JS ", $ CCC: $. core. base + "CCC. JS ", $ DDD: $. core. base + "DDD/DDD. JS "}})

Then reference the DDD. js module on the page.

<! Doctype HTML>  

Correspondingly, these JS modules can also be changed

 
// AAA. no JS dependency. You do not need to change define ("AAA", function () {return 1}) // BBB. no JS dependency. You do not need to change define ("BBB", function () {return 2}); // CCC. jsdefine ("CCC", ["$ AAA"], function (a) {return 10 + A}) // DDD/DDD. jsdefine ("DDD", ["$ DDD"], function (c) {return C + 100 });

Then it will pop up correctly 111!

If one day we move DDD. js to another directory, for example, http: // xdsfsd/sadfs/DDD. JS, we just need to change the modules directory! You do not need to change $. require or the dependency list of DDD!

 
// $. Core. base is mass. JS directory $. config ({alias: {$ AAA: $. core. base + "AAA. JS ", $ BBB: $. core. base + "BBB. JS ", $ CCC: $. core. base + "CCC. JS ", $ DDD:" http: // xdsfsd/sadfs/DDD. JS "}})

Now let's take a look at the use of jquery and Mass. js. Although jquery has added several lines to support AMD, it is actually very rare! However, the coexistence of multiple libraries in jquery is very good! We only need a layer of bread to use. Download the jquery source code from the official jquery website, whether it is a compressed version or a comment version. Create a new jquery. js file in the same directory as mass. js. The content is as follows:

 
Define ("jquery", function () {// ********** jquery source code ********** return jquery. noconflict (true );})
<! Doctype HTML>  

When we open firebug, we can see the following logs:

The use of jquery plug-ins is also an outsourcing Layer

 (function ($) $. FN. overlabel = function () {This. each (function (INDEX) {var label = $ (this); var field; var id = this.html for | label. ATTR ('for'); If (ID & (field = document. getelementbyid (ID) {var control = $ (field); label. addclass ("overlabel-Apply"); If (field. value! = '') {Label.css (" text-indent ","-1000px ");} control. focus (function () extends label.css ("text-indent", "-1000px ");}). blur (function () {If (this. value = '') {label.css (" text-indent "," 0px ") ;}}); label. click (function () {var label = $ (this); var field; var id = this.html for | label. ATTR ('for'); If (ID & (field = document. getelementbyid (ID) {field. focus () ;}}) ;}}( jquery) 

Transformed

 
Define ("jquery_overlabel ",[". /jquery "], function (jquery) {(function ($) {// *************}) (jquery );})

This section is complete!

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.