Created
June 10, 2014 14:16
-
-
Save geastwood/ecf974914979f69730d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function() { | |
var ScriptLoader = function(url, ns, onLoadCallback, name) { | |
this.url = url; // url of the module | |
this.ns = ns; // source obj/ns obj to bind 'define' method | |
this.onLoadCallback = onLoadCallback; | |
this.name = name; // dependency name | |
this.load(); | |
}; | |
ScriptLoader.prototype.timer = function timer(times, // number of times to try | |
delay, // delay per try | |
delayMore, // extra delay per try (additional to delay) | |
test, // called each try, timer stops if this returns true | |
failure, // called on failure | |
result // used internally, shouldn't be passed | |
) { | |
console.log('in timer'); | |
if (times == -1 || times > 0) { | |
setTimeout(function () { | |
result = (test()) ? 1 : 0; | |
timer((result) ? 0 : (times > 0) ? --times : times, delay + ((delayMore) ? delayMore : 0), delayMore, test, failure, result); | |
}, (result || delay < 0) ? 0.1 : delay); | |
} else if (typeof failure == 'function') { | |
setTimeout(failure, 1); | |
} | |
}; | |
ScriptLoader.prototype.addEvent = function (el, eventName, eventFunc) { | |
if (typeof el != 'object') { | |
return false; | |
} | |
if (el.addEventListener) { | |
el.addEventListener(eventName, eventFunc, false); | |
return true; | |
} | |
if (el.attachEvent) { | |
el.attachEvent("on" + eventName, eventFunc); | |
return true; | |
} | |
return false; | |
}; | |
ScriptLoader.prototype.load = function() { | |
var that = this; | |
var delay; | |
var script = document.createElement('script'); | |
var head = document.head || document.getElementsByTagName('head')[0]; | |
if (!head) { | |
return false; | |
} | |
Modulerjs.bindDefine(this.ns); | |
setTimeout(function () { | |
var fail = function () { | |
if (!script.__es) { | |
script.__es = true; | |
script.id = 'failed'; | |
that.fail.call(script); | |
} | |
}; | |
script.onload = function () { | |
script.id = 'loaded'; | |
that.onLoadCallback(that.name); | |
}; | |
if (script.readyState) { | |
script.onreadystatechange = function() { | |
if (script.readyState == 'loaded' || script.readyState == 'complete') { | |
script.id = 'loaded'; | |
script.onreadystatechange = null; | |
} | |
}; | |
} | |
script.type = 'text/javascript'; | |
// script.async = (typeof args.async == 'boolean') ? args.async : false; | |
script.charset = 'utf-8'; | |
that.__es = false; | |
that.addEvent(script, 'error', fail); // when supported | |
// when error event is not supported fall back to timer | |
that.timer(3, 1000, 0, function () { | |
return (script.id == 'loaded'); | |
}, function () { | |
if (script.id != 'loaded') { | |
fail(); | |
} | |
}); | |
script.src = that.url; | |
setTimeout(function () { | |
try { | |
head.appendChild(script); | |
} catch (e) { | |
fail(); | |
} | |
}, 1); | |
}, (typeof delay == 'number') ? delay : 1); | |
return true; | |
}; | |
ScriptLoader.prototype.fail = function() { | |
throw new Error('script load error url: ' + this.src); | |
}; | |
return ScriptLoader; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment