Require.js 2.0

Source: Internet
Author: User
Tags define definition

Just the night before the Requirejs released a large version, directly from the version1.0.8 upgrade to 2.0. The next few hours James Burke quickly adjusted the version to 2.0.1, of course, the package compression tool R.js also upgraded to 2.0.1. The changes are large, the code is refactored, and the levels are clearer and more readable. The main functional changes are as follows:

1, delay the execution of the module.

This is a big change, before the module is loaded immediately after the factory executes. There must be some loss in performance. 2.0 Modify the implementation, no one criticized the AMD module is immediately executed. It is now possible to wait until require to execute.

2,config increased the shim,map,module,enforcedefine.

The shim parameter resolves modules that are defined using non-AMD methods, such as jquery plug-ins, and their loading order. Use the shim parameter to replace the 1.0 version of the order plugin. In fact, the use and wrap plugins have been developed in version 1.0 to solve such problems. Considering that many developers have such requirements (for example, some JS modules were developed earlier by others, non-AMD way) This 2.0 version is built directly into it.

Here is a parameter configured using the jquery plug-in form. We know that jquery plugins essentially hang namespaces on global jquery or JQUERY.FN rather than modules defined with define. jquery plugins rely on jquery, which means that jquery is the first to be downloaded when require plugins. Can be configured as follows

123456 require.config({    shim: {        ‘jquery-slide‘: [‘jquery‘]    }});require([‘jquery-slide‘]);

You will then be guaranteed to download the jquery.js before downloading the jquery-slide.js.

The map parameter is used to solve different versions of the same module, which is inspired by the packagemap of dojo. There is a scenario in which the initial use of jquery-1.6.4 was developed and later upgraded to 1.7.2. But there are concerns that some of the jquery-1.6.4-dependent code has been upgraded to 1.7.2. So keep this part of the code conservative to continue using the 1.6.4 version. The map parameter will be useful at this point.

If the Jquery-1.6.4.js,c,d module is used in A/b module, the jquery-1.7.2.js is used. As follows

123456789101112 requirejs.config({    map: {        ‘A‘: {            ‘jquery‘‘jquery-1.6.4‘        },        ‘B‘: {            ‘jquery‘‘jquery-1.7.2‘        }    }});require([‘A‘]); // download jquery-1.6.4.jsrequire([‘B‘]); // download jquery-1.7.2.js

then require ([' A ']) will download jquery-1.6.4.js,require ([' B ']) to download jquery-1.7.2.js. The module "A", if written as "*", indicates that the module uses jquery-1.6.4 in addition to the B module using jquery-1.7.2. The map parameter solves the problem of each version of the module, which is well backwards compatible with the old code.

The config parameter is used to pass some useful data to the specified module. As follows

1234567 require.config({    config: {        ‘A‘: {            info: {name: ‘jack‘}        }    }});

This data information can be obtained through A.config (). Info in the module using a. Such as

1234 require([‘A‘], function(A) {    varinfo = a.config().info;    console.log(info);});

The enforcedefine is used to force the module to use the Define definition, which defaults to false. If underscore no longer supports AMD, its code removes define. At this point, if you still use Requirejs to load it, it is the normal JS file. If Enforcedefine is set to true at this point, although underscore.js can download it, Requirejs will give an error. Such as

123456 require.config({    enforceDefine: true});require([‘underscore‘], function(_){    console.log(_)})

Error message

The 4,require function adds a third parameter, Errbacks.

It is obvious that this function refers to a callback when the module file is not loaded successfully. This is also expected by some developers to be required to add, including another famous AMD implementation of Curl author John Hann.

12345 require([‘b‘], function(){    console.log(‘success‘);},function(err){    console.log(err)});

Err will give you some information about the error.

5, more powerful paths parameters.

The Requirejs 1.x version already has the paths parameter, which is used to map module aliases. Requirejs2.0 is more powerful and can be configured as an array, sequential mapping. You can then use the following path when the previous path is not successfully loaded. As follows

123456789101112 requirejs.config({    enforceDefine: true,    paths: {        jquery: [            ‘http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min‘,            ‘lib/jquery‘        ]    }});require([‘jquery‘], function($) {});

When Jquery.min.js on a Google CDN is not available (if Google goes down), you can use the local lib/jquery.js.

6, the module registration can be removed using the UNDEF function in the module load failure callback.

This inspiration comes from the dojo AMD Loader,requirejs named Undef. As follows

12345678 require([‘jquery‘], function($) {    //Do something with $ here}, function(err) {    varfailedId = err.requireModules && err.requireModules[0];    if(failedId === ‘jquery‘) {        requirejs.undef(failedId);    }});

7, removed the jquery domready related code.

This time no one ever criticized Requirejs and jquery too tightly coupled.

8, removed the priority,packagepaths,catcherror.define.

These parameters have been replaced by the corresponding alternatives.

Finally, it is important to note that although the function has increased a lot. But the code volume was reduced by nearly 60 lines. The main is to remove the jquery ready related code. There are still more than 1000 lines in the Newcontext function.

Require.js 2.0

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.