curl-amd
Version:
curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
86 lines (64 loc) • 1.85 kB
HTML
<html>
<head>
<title>load module once test file</title>
<script>
curl = {
baseUrl: '',
paths: {
curl: '../src/curl',
jquery: '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min'
}
};
</script>
<script src="../src/curl.js" type="text/javascript"></script>
<script type="text/javascript">
curl(['curl/_privileged', 'tdd/MethodSpy', 'domReady!'], function (priv, MethodSpy) {
// override curl's insertScript function with an injected spy
var core = priv.core,
origLoadScript = core.loadScript,
Promise = priv.Promise,
module1 = 'stuff/one',
module2 = 'jquery';
function runTest (moduleId, howMany) {
var spy = new MethodSpy('loadScript', core),
promise = new Promise(),
count = 2;
function countdown () {
if (--count == 0) {
write((spy.calledMany(howMany || 0) ? 'SUCCESS' : 'FAILED') + ': should load ' + moduleId + ' exactly once');
priv.core.loadScript = origLoadScript;
promise.resolve();
}
}
curl([moduleId], function (whatevs) {}).then(
countdown,
function (ex) {
write('FAILED: on ' + moduleId + ' exception:' + ex);
}
);
curl([moduleId], function (whatevs) {}).then(
countdown,
function (ex) {
write('FAILED: on ' + moduleId + ' exception:' + ex);
}
);
return promise;
}
window.runTest = runTest;
window.module1 = module1;
window.module2 = module2;
runTest(module1, 1).then(function () {
runTest(module2, 1);
});
});
function write (msg) {
document.body.appendChild(document.createElement('div')).innerHTML = msg;
}
</script>
</head>
<body>
<button onclick="runTest(window.module1);">press this button to load the local module again</button>
<button onclick="runTest(window.module2);">press this button to load the remote module again</button>
</body>
</html>