Optimized version 1.0 of the M3M4 loader to meet basic needs

Source: Internet
Author: User

 

Optimized version 1.0 of the M3M4 loader to meet basic needs

/*//1, a module name that begins with $, represents a service, and the service executes only once, and it can have its own method.    For example, a cookie is a service//such as: Console.log ("root"); only once define ("$root", [function () {Console.log ("root") is executed; Return "Root"}])//2, not the beginning of the normal module, the normal module every time the call will be executed//For example: Console.log ("userlist"), each time will execute define ("UserList", ["$root",    function ($root) {console.log ($root);    Console.log ("userlist"); Return "UserList"}])//3, Presence $callback represents the asynchronous callback module, $callback will return the asynchronous data define ("Sync1", ["$root", "$callback", function (A, Callback) {Console.log () setTimeout (function () {Callback ("1 sync")},1000)}]) require ("Sync1", function (SYNC1) {Console.log (SYNC1);//Output result: 1 sync}) * */(function ($) {var models={} var baseloaded={} var basecache={} function define (Name,args) {Models[n Ame]=args} function require (Name,callback) {var curcache=object.create (Basecache) var curloaded=objec T.create (baseloaded) function Currequire (name,callback) {//Return from Global if (/^\$/.test (name) &&am      P;baseloaded[name]) {          Callback (Basecache[name]) return}//return result if (Curloaded[name]) { Callback (Curcache[name])//Set Global if (/^\$/.test (name)) {Basec            Ache[name]=curcache[name] Baseloaded[name]=curloaded[name]} return            } var next=function () {Currequire (Name,callback)}//Get module length var args=models[name] var func=args[args.length-1] Var len=args.length-1 var dataarr=[ ]//Filter $callback var sync=args.indexof ("$callback") if (sync!=-1) {dataarr[ Sync]=function (back) {curcache[name]=back curloaded[name]=true NE XT ()}--len} if (len==0) {curcache[name]= func.apply (n            Ull,dataarr)    Curloaded[name]=true Next ()}else{for (Var i=0;i<args.length-1;i++) {                    if (I==sync) continue;                            (function (N,model) {Currequire (model,function (data) {Dataarr[n]=data if (--len==0) {if (sync!=-1) {func . Apply (Null,dataarr)}else{curcache[name]=func.apply (nu                                Ll,dataarr) Curloaded[name]=true Next ()                }}) (I,args[i]) }}} Currequire (Name,callback)} $.m3m4=function (func) {func (Define,require)}    ;    $.define=define; $.require=require;}) (typeof window!= "undefined"? window:global)

  

Demo

M3M4 (function (define,require) {    //service    define ("$root", [function () {        Console.log ("executes only once")        return "$ Root "    }])    //Based on $root configuration, asynchronously loads user 1 data    define (" User1 ", [" $root "," $callback ", Function ($root, callback) {        setTimeout (function () {            callback ("user1" + $root)        },1000)    }])    //synchronous loading of user user2 data    define ("User2", ["$root", function ($root) {        return "User2"    }])    //Organize into user list    define ("UserList", ["User1", "User2", function (user1,user2) {        console.log (user1)        Console.log (user2)        return {            user1:user1,            user2:user2        }    }])    Gets the data in the user list    require ("UserList", function (userlist) {        console.log (userlist)    })    require (" UserList ", function (userlist) {        console.log (userlist)})    })

  

Optimized version 1.0 of the M3M4 loader to meet basic needs

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.