返回列表

global require define exports

默认分类 2013/10/04 07:20

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<script type="text/javascript">
'use strict';
if (typeof (require) == 'undefined') {
    var require = function(url, onload) { 
        var exist = false;
        for (var i=0,len=define.loaded.length; i<len; i++) {
            if (define.loaded[i] == url) {
                exist = true;
                define.checkDepend();
                console.log(url);

                typeof (onload) == 'function' ? onload() : (new Function(onload))();
            }
        }

        if (exist) { return;}

        var callback = function() { 
            // check depends
            if (typeof (define) != 'undefined' && define.checkDepend) {
                define.loaded.push(url);

                define.checkDepend();
                console.log(url);
            }

            typeof (onload) == 'function' ? onload() : (new Function(onload))();
        };

        require.onloadStack = window.require.onloadStack || [];
        require.onloadStack.push(callback);        

        if (document.body != null || (document.getElementsByTagName && document.getElementsByTagName('body')[0] != null)) { 
            //write ready function here 
            var domscript = document.createElement('script'); 
            domscript.src = url; 
            domscript.onload = callback; 
            document.getElementsByTagName('head')[0].appendChild(domscript); 
        }
        else {
            document.write('<script type="text/javascript" src="' + url + '"' +
            ' onload="window.require.onloadStack[' + (window.require.onloadStack.length-1) + ']()"><'+'/script>'); 
        }
    };
}

if (typeof (define) == 'undefined') {
    var define = function (name, deps, callback) {
        //Allow for anonymous modules
        if (typeof name !== 'string') {
            //Adjust args appropriately
            callback = deps;
            deps = name;
            name = null;
        }

        //This module may not have dependencies
        if (!deps || !deps.splice) {
            callback = deps;
            deps = null;
        }

        deps = deps && deps.splice && deps.length ? deps : [];

        define.modules = define.modules || [];
        define.modules.push({
            name: name, 
            depend: deps, 
            left: deps.join(',').replace(/[ ]/g, ''), 
            todo: callback, 
            exports: {}
        });
        for (var i=0,len=deps.length; i<len; i++) {
            require(deps[i]);
        }
    };
    define.checkDepend = function () {
        define.modules = define.modules || [];
        var list = define.modules,
            loaded = define.loaded,
            url;
        for (var j=0,len2=loaded.length; j<len2; j++) {
            url = loaded[j];
            for (var i=0,len=list.length; i<len; i++) {
                console.log(url);
                list[i].left = list[i].left.replace(','+url, '').replace(url+',', '').replace(url, '');
                console.log(list[i].left);
                if (list[i].left.length<1) {
                    list[i].todo(list[i].exports);
                }
            }
        }
    };
    define.loaded = [];
};


require('./2.js');
/*require('./htmlparser.js');
require('./control.js');
require('./button-group.js');
debugger;
var haiyang = bui.Control.init("<div ui=\"type:'ButtonGroup',id:'cost',name:'cost',value:'50-100',options:['0-50','50-100','100-150','150-200','200+']\"></div>");
console.log(haiyang);

function doit(){
    //Todo

}
*/


//-->
</script>
</head>

<body><button type="button" onclick="doit()">doit</button>

</body>

</html>


'use strict';

//b.js
define('./b.js', ['./a.js'], function(exports){
    var dd = {};
    dd.name = 'out-b';
    dd.out = function () {
        alert(dd.name);
    };
    exports.out = dd.out;
    console.log(exports);
});

'use strict';
//a.js
define('./a.js', [], function(exports){
    var dd = {};
    dd.name = 'out-a';
    dd.out = function () {
        alert(dd.name);
    };
    exports.out = dd.out;
    console.log(exports);
});