REQUIREJS Code Structure Analysis

Source: Internet
Author: User

One. function entry function.

req = Requirejs =function(Deps, Callback, errback, optional) {//Find The right context, use default        varcontext, config, Contextname= Defcontextname;//"_"        //If the deps is an object, it may be config.         //determine if has config object in the call.        if(!isarray (Deps) &&typeofDeps!== ' string ') {            //Deps is a config objectConfig =Deps; if(IsArray (callback)) {//Adjust args If there are dependenciesDeps =callback; Callback=Errback; Errback=Optional; } Else{deps= []; }        }        if(Config &&config.context) {contextname=Config.context; }        //gets its own properties.         //Contexts: Operating environment. Default "-"Context =Getown (contexts, contextname); if(!Context) {
Returns the core function, Context.require = Context.makerequire (), which determines the Require function. Context
= Contexts[contextname] =Req.s.newcontext (contextname); } if(config) {context.configure (config); }//This function is run to get Makerequire, will go to the dependencies from define memory to find out, and instantiate the module,returnContext.require (Deps, Callback, Errback); };

Create default context.

req ({}); //Create a default environment, contexts["_"], and return a require (deps,callback,errback) for external use.

CFG: The first to determine the good baseurl, or the value of dependent data-main will be placed in the dependency deps this property. Cfg.deps will call the Require function to check and download.

Req (CFG); Set up with config info, which introduces the configuration of CFG BaseURL, data-main to dependent cfg.deps.

Newcontext returns the core function: Context.require = Context.makerequire ();

Makerequire: is the function entry core function: Req ({}), which determines the good environment.

Makerequire's core code:

Context.nexttick (function () {                        //Some defines could have been added since the                        //require call, collect them.Console.log ( This, Deps); //take out and instantiate the object from the Define function registration and initIntakedefines (); //take or create an instantiation module from registerRequiremod = GetModule (Makemodulemap (NULL, Relmap)); //Store If map config should is applied to this require                        //Call for dependencies.Requiremod.skipmap =Options.skipmap;                        tag dependencies, and download. Requiremod.init (Deps, callback, Errback, {enabled:true                        }); //determine if the download has been made, or the super-times error, or not timed out to cycle the download OK. checkloaded (); });

Parsing of the module function:

role: determine dependencies, and dependencies define and execute, listen onscriptload, and download. Finally, the exposed function is executed.

The main functions are the following:

Enable function: Tag dependent function, tag number depcount, and dependency makemodulemap (Uniform processing path, ID, plug-in header, URL, whether defined. This form. ), and then check again to see if it is executable (check ()).

Enable (): Organize the Flags of the dependencies (number, etc.),->check ()-The first time is a download (the download completes automatically loops to detect whether the download is complete. ), two times is to reduce the dependency and execute the exposure function.

Check: function://check before you make sure that you have the enable (process dependency, instance dependency module), check that there is no dependency, and run exports.

Fetch: Function: (Listening: Onscriptload)-"completeload--" checkloaded

Completeload: function:

Completeload: from the Define defined module to get the corresponding instantiation module, if not, then define from Shim and instantiate the module otherwise error. and check for downloads.

checkloaded: function:

Checkloaded: Determine if the download, or the super-times error, or did not time out to cycle the download OK.

Define function: function: Simply add the defined module to the memory. Wait until the require trigger is taken out of the corresponding memory.

Cond:

REQUIREJS Code Structure Analysis

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.