Permalink
Browse files

fix bug

  • Loading branch information...
1 parent c7b0e10 commit 6d797990dfb15e395cae056cf92c4647e3ddfdfa @kejun committed Jul 11, 2011
Showing with 32 additions and 47 deletions.
  1. +30 −45 do.js
  2. +2 −2 do.min.js
View
75 do.js
@@ -1,6 +1,6 @@
/* Do version 2.0 pre
* creator: kejun (listenpro@gmail.com)
- * 最新更新:2011-5-16
+ * 最新更新:2011-7-12
*/
(function(win, doc) {
@@ -64,6 +64,15 @@ isArray = function(e) {
return e.constructor === Array;
},
+getMod = function(e) {
+ var mods = config.mods, mod;
+ if (typeof e === 'string') {
+ mod = (mods[e])? mods[e] : { path: e };
+ } else {
+ mod = e;
+ }
+ return mod;
+},
load = function(url, type, charset, cb) {
var wait, n, t, img,
@@ -157,9 +166,7 @@ load = function(url, type, charset, cb) {
};
}
- setTimeout(function(){
- jsSelf.parentNode.insertBefore(n, jsSelf);
- }, 0);
+ jsSelf.parentNode.insertBefore(n, jsSelf);
},
// 加载依赖论文件(顺序)
@@ -183,7 +190,7 @@ loadDeps = function(deps, cb) {
}
for (; m = deps[i++]; ) {
- mod = (mods[m])? mods[m] : { path: m };
+ mod = getMod(m);
if (mod.requires) {
loadDeps(mod.requires, (function(mod){
return function(){
@@ -256,63 +263,41 @@ fireReadyList = function() {
},
d = function() {
- var args = [].slice.call(arguments),
- mods = config.mods, fn, list, id, len, i = 0, m, mod;
+ var args = [].slice.call(arguments), fn, id;
// 加载核心库
- if (config.autoLoad) {
- if (!loadList[config.coreLib.join('')]) {
- loadDeps(config.coreLib, function(){
- d.apply(null, args);
- });
- return;
- }
+ if (config.autoLoad &&
+ !loadList[config.coreLib.join('')]) {
+ loadDeps(config.coreLib, function(){
+ d.apply(null, args);
+ });
+ return;
}
- // 加载核心库
- if (globalList.length > 0) {
- if (!loadList[globalList.join('')]) {
- loadDeps(globalList, function(){
- d.apply(null, args);
- });
- return;
- }
+ // 加载全局库
+ if (globalList.length > 0 &&
+ !loadList[globalList.join('')]) {
+ loadDeps(globalList, function(){
+ d.apply(null, args);
+ });
+ return;
}
-
if (typeof args[args.length - 1] === 'function' ) {
fn = args.pop();
}
id = args.join('');
-
if ((args.length === 0 || loadList[id]) && fn) {
fn();
return;
}
- len = args.length;
-
- function callback() {
- if (!--len) {
- loadList[id] = 1;
- fn && fn();
- }
- };
-
- for (; m = args[i++]; ) {
- mod = (mods[m])? mods[m] : { path: m };
- if (mod.requires) {
- loadDeps(mod.requires, (function(mod){
- return function(){
- load(mod.path, mod.type, mod.charset, callback);
- };
- })(mod));
- } else {
- load(mod.path, mod.type, mod.charset, callback);
- }
- }
+ loadDeps(args, function() {
+ loadList[id] = 1;
+ fn && fn();
+ });
};
d.add = function(sName, oConfig) {
View
4 do.min.js
@@ -1,4 +1,4 @@
/* Do version 2.0 pre
* creator: kejun (listenpro@gmail.com)
- * 最新更新:2011-5-16
- */(function(win,doc){var loaded={},loadList={},loadingFiles={},config={autoLoad:!0,timeout:6e3,coreLib:["http://t.douban.com/js/jquery.min.js"],mods:{}},jsSelf=function(){var a=doc.getElementsByTagName("script");return a[a.length-1]}(),globalList=[],extConfig,readyList=[],isReady=!1,publicData={},publicDataStack={},isArray=function(a){return a.constructor===Array},load=function(url,type,charset,cb){var wait,n,t,img,done=function(){loaded[url]=1,cb&&cb(url),cb=null,win.clearTimeout(wait)};if(url){if(loaded[url]){loadingFiles[url]=!1,cb&&cb(url);return}if(loadingFiles[url]){setTimeout(function(){load(url,type,charset,cb)},10);return}loadingFiles[url]=!0,wait=win.setTimeout(function(){if(config.fallback)try{eval(config.fallback)(url)}catch(ex){}},config.timeout),t=type||url.toLowerCase().substring(url.lastIndexOf(".")+1),t==="js"?(n=doc.createElement("script"),n.setAttribute("type","text/javascript"),n.setAttribute("src",url),n.setAttribute("async",!0)):t==="css"&&(n=doc.createElement("link"),n.setAttribute("type","text/css"),n.setAttribute("rel","stylesheet"),n.setAttribute("href",url)),charset&&(n.charset=charset),t==="css"?(img=new Image,img.onerror=function(){done(),img.onerror=null,img=null},img.src=url):(n.onerror=function(){done(),n.onerror=null},n.onload=n.onreadystatechange=function(){var a;if(!this.readyState||this.readyState==="loaded"||this.readyState==="complete")done(),n.onload=n.onreadystatechange=null}),setTimeout(function(){jsSelf.parentNode.insertBefore(n,jsSelf)},0)}},loadDeps=function(a,b){function i(){--h||(loadList[d]=1,b())}var c=config.mods,d,e,f,g=0,h;d=a.join(""),h=a.length;if(loadList[d])b();else for(;e=a[g++];)f=c[e]?c[e]:{path:e},f.requires?loadDeps(f.requires,function(a){return function(){load(a.path,a.type,a.charset,i)}}(f)):load(f.path,f.type,f.charset,i)},contentLoaded=function(a){var b=!1,c=!0,d=win.document,e=d.documentElement,f=d.addEventListener?"addEventListener":"attachEvent",g=d.addEventListener?"removeEventListener":"detachEvent",h=d.addEventListener?"":"on",i=function(c){if(c.type!="readystatechange"||d.readyState=="complete")(c.type=="load"?win:d)[g](h+c.type,i,!1),!b&&(b=!0)&&a.call(win,c.type||c)},j=function(){try{e.doScroll("left")}catch(a){setTimeout(j,50);return}i("poll")};if(d.readyState=="complete")a.call(win,"lazy");else{if(d.createEventObject&&e.doScroll){try{c=!win.frameElement}catch(k){}c&&j()}d[f](h+"DOMContentLoaded",i,!1),d[f](h+"readystatechange",i,!1),win[f](h+"load",i,!1)}},fireReadyList=function(){var a=0,b;if(readyList.length)for(;b=readyList[a++];)d.apply(this,b)},d=function(){function k(){--g||(loadList[f]=1,c&&c())}var a=[].slice.call(arguments),b=config.mods,c,e,f,g,h=0,i,j;if(config.autoLoad)if(!loadList[config.coreLib.join("")]){loadDeps(config.coreLib,function(){d.apply(null,a)});return}if(globalList.length>0)if(!loadList[globalList.join("")]){loadDeps(globalList,function(){d.apply(null,a)});return}typeof a[a.length-1]==="function"&&(c=a.pop()),f=a.join("");if((a.length===0||loadList[f])&&c)c();else{g=a.length;for(;i=a[h++];)j=b[i]?b[i]:{path:i},j.requires?loadDeps(j.requires,function(a){return function(){load(a.path,a.type,a.charset,k)}}(j)):load(j.path,j.type,j.charset,k)}};d.add=function(a,b){a&&b&&b.path&&(config.mods[a]=b)},d.delay=function(){var a=[].slice.call(arguments),b=a.shift();win.setTimeout(function(){d.apply(this,a)},b)},d.global=function(){var a=isArray(arguments[0])?arguments[0]:[].slice.call(arguments);globalList=globalList.concat(a)},d.ready=function(){var a=[].slice.call(arguments);if(isReady)return d.apply(this,a);readyList.push(a)},d.css=function(a){var b=doc.getElementById("do-inline-css");b||(b=doc.createElement("style"),b.type="text/css",b.id="do-inline-css",jsSelf.parentNode.insertBefore(b,jsSelf)),b.styleSheet?b.styleSheet.cssText=b.styleSheet.cssText+a:b.appendChild(doc.createTextNode(a))},d.set=d.setPublicData=function(a,b){var c=publicDataStack[a];publicData[a]=b;if(c)while(c.length>0)c.pop().call(this,b)},d.get=d.getPublicData=function(a,b){publicData[a]?b(publicData[a]):(publicDataStack[a]||(publicDataStack[a]=[]),publicDataStack[a].push(function(a){b(a)}))},d.setConfig=function(a,b){config[a]=b;return d},d.getConfig=function(a){return config[a]},win.Do=d,contentLoaded(function(){isReady=!0,fireReadyList()}),extConfig=jsSelf.getAttribute("data-cfg-autoload"),extConfig&&(config.autoLoad=extConfig.toLowerCase()==="true"?!0:!1),extConfig=jsSelf.getAttribute("data-cfg-corelib"),extConfig&&(config.coreLib=extConfig.split(","))})(window,document)
+ * 最新更新:2011-7-12
+ */(function(win,doc){var loaded={},loadList={},loadingFiles={},config={autoLoad:!0,timeout:6e3,coreLib:["http://t.douban.com/js/jquery.min.js"],mods:{}},jsSelf=function(){var a=doc.getElementsByTagName("script");return a[a.length-1]}(),globalList=[],extConfig,readyList=[],isReady=!1,publicData={},publicDataStack={},isArray=function(a){return a.constructor===Array},getMod=function(a){var b=config.mods,c;typeof a=="string"?c=b[a]?b[a]:{path:a}:c=a;return c},load=function(url,type,charset,cb){var wait,n,t,img,done=function(){loaded[url]=1,cb&&cb(url),cb=null,win.clearTimeout(wait)};if(!!url){if(loaded[url]){loadingFiles[url]=!1,cb&&cb(url);return}if(loadingFiles[url]){setTimeout(function(){load(url,type,charset,cb)},10);return}loadingFiles[url]=!0,wait=win.setTimeout(function(){if(config.fallback)try{eval(config.fallback)(url)}catch(ex){}},config.timeout),t=type||url.toLowerCase().substring(url.lastIndexOf(".")+1),t==="js"?(n=doc.createElement("script"),n.setAttribute("type","text/javascript"),n.setAttribute("src",url),n.setAttribute("async",!0)):t==="css"&&(n=doc.createElement("link"),n.setAttribute("type","text/css"),n.setAttribute("rel","stylesheet"),n.setAttribute("href",url)),charset&&(n.charset=charset),t==="css"?(img=new Image,img.onerror=function(){done(),img.onerror=null,img=null},img.src=url):(n.onerror=function(){done(),n.onerror=null},n.onload=n.onreadystatechange=function(){var a;if(!this.readyState||this.readyState==="loaded"||this.readyState==="complete")done(),n.onload=n.onreadystatechange=null}),jsSelf.parentNode.insertBefore(n,jsSelf)}},loadDeps=function(a,b){function i(){--h||(loadList[d]=1,b())}var c=config.mods,d,e,f,g=0,h;d=a.join(""),h=a.length;if(loadList[d])b();else for(;e=a[g++];)f=getMod(e),f.requires?loadDeps(f.requires,function(a){return function(){load(a.path,a.type,a.charset,i)}}(f)):load(f.path,f.type,f.charset,i)},contentLoaded=function(a){var b=!1,c=!0,d=win.document,e=d.documentElement,f=d.addEventListener?"addEventListener":"attachEvent",g=d.addEventListener?"removeEventListener":"detachEvent",h=d.addEventListener?"":"on",i=function(c){if(c.type!="readystatechange"||d.readyState=="complete")(c.type=="load"?win:d)[g](h+c.type,i,!1),!b&&(b=!0)&&a.call(win,c.type||c)},j=function(){try{e.doScroll("left")}catch(a){setTimeout(j,50);return}i("poll")};if(d.readyState=="complete")a.call(win,"lazy");else{if(d.createEventObject&&e.doScroll){try{c=!win.frameElement}catch(k){}c&&j()}d[f](h+"DOMContentLoaded",i,!1),d[f](h+"readystatechange",i,!1),win[f](h+"load",i,!1)}},fireReadyList=function(){var a=0,b;if(readyList.length)for(;b=readyList[a++];)d.apply(this,b)},d=function(){var a=[].slice.call(arguments),b,c;if(config.autoLoad&&!loadList[config.coreLib.join("")])loadDeps(config.coreLib,function(){d.apply(null,a)});else{if(globalList.length>0&&!loadList[globalList.join("")]){loadDeps(globalList,function(){d.apply(null,a)});return}typeof a[a.length-1]=="function"&&(b=a.pop()),c=a.join("");if((a.length===0||loadList[c])&&b){b();return}loadDeps(a,function(){loadList[c]=1,b&&b()})}};d.add=function(a,b){!a||!b||!b.path||(config.mods[a]=b)},d.delay=function(){var a=[].slice.call(arguments),b=a.shift();win.setTimeout(function(){d.apply(this,a)},b)},d.global=function(){var a=isArray(arguments[0])?arguments[0]:[].slice.call(arguments);globalList=globalList.concat(a)},d.ready=function(){var a=[].slice.call(arguments);if(isReady)return d.apply(this,a);readyList.push(a)},d.css=function(a){var b=doc.getElementById("do-inline-css");b||(b=doc.createElement("style"),b.type="text/css",b.id="do-inline-css",jsSelf.parentNode.insertBefore(b,jsSelf)),b.styleSheet?b.styleSheet.cssText=b.styleSheet.cssText+a:b.appendChild(doc.createTextNode(a))},d.set=d.setPublicData=function(a,b){var c=publicDataStack[a];publicData[a]=b;if(!!c)while(c.length>0)c.pop().call(this,b)},d.get=d.getPublicData=function(a,b){publicData[a]?b(publicData[a]):(publicDataStack[a]||(publicDataStack[a]=[]),publicDataStack[a].push(function(a){b(a)}))},d.setConfig=function(a,b){config[a]=b;return d},d.getConfig=function(a){return config[a]},win.Do=d,contentLoaded(function(){isReady=!0,fireReadyList()}),extConfig=jsSelf.getAttribute("data-cfg-autoload"),extConfig&&(config.autoLoad=extConfig.toLowerCase()==="true"?!0:!1),extConfig=jsSelf.getAttribute("data-cfg-corelib"),extConfig&&(config.coreLib=extConfig.split(","))})(window,document)

0 comments on commit 6d79799

Please sign in to comment.