Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Pass dependencies through to legacy factory. Fixes #270.

  • Loading branch information...
commit 662138c98041249c94542c3e41c18c1dd4bb4476 1 parent 65dc25b
@unscriptable unscriptable authored
Showing with 35 additions and 17 deletions.
  1. +20 −12 src/curl/loader/legacy.js
  2. +15 −5 test/legacy.html
View
32 src/curl/loader/legacy.js
@@ -95,7 +95,8 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
return {
'load': function (resId, require, callback, cfg) {
- var exports, factory, deps, dontAddFileExt, url, options, countdown;
+ var exports, factory, depIds, dontAddFileExt, url, options,
+ countdown, deps;
exports = cfg['exports'] || cfg.exports;
factory = cfg['factory'] || cfg.factory;
@@ -103,7 +104,8 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
throw new Error('`exports` or `factory` required for legacy: ' + resId);
}
- deps = [].concat(cfg['requires'] || cfg.requires || []);
+ depIds = [].concat(cfg['requires'] || cfg.requires || []);
+ deps = [];
dontAddFileExt = cfg['dontAddFileExt'] || cfg.dontAddFileExt;
dontAddFileExt = dontAddFileExt
? new RegExp(dontAddFileExt)
@@ -119,7 +121,7 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
order: true,
// set a fake mimetype if we need to wait and don't support
// script.async=false.
- mimetype: hasAsyncFalse || !deps.length ? '' : 'text/cache'
+ mimetype: hasAsyncFalse || !depIds.length ? '' : 'text/cache'
};
// hasAsyncFalse, nodeps: load | _export
@@ -127,7 +129,7 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
// !hasAsyncFalse, nodeps: load | _export
// !hasAsyncFalse, deps: getDeps+load | reload | _export
- if (deps.length) {
+ if (depIds.length) {
countdown = 2;
getDeps();
load();
@@ -138,8 +140,7 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
}
function getDeps () {
- // start process of getting deps, then either export or reload
- require(deps, hasAsyncFalse ? _export : reload, reject);
+ require(depIds, _continue, reject);
}
function load () {
@@ -147,11 +148,18 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
loadScript(options, _export, reject);
}
- function reload () {
- // if we faked the mimetype, we need to refetch.
- // (hopefully, from cache, if cache headers allow.)
- options.mimetype = '';
- loadScript(options, _export, reject);
+ function _continue () {
+ // save dependencies
+ deps = arguments;
+ if (!hasAsyncFalse) {
+ // if we faked the mimetype, we need to refetch.
+ // (hopefully, from cache, if cache headers allow.)
+ options.mimetype = '';
+ load();
+ }
+ else {
+ _export();
+ }
}
function _export () {
@@ -159,7 +167,7 @@ define(/*=='curl/loader/legacy',==*/ ['curl/_privileged'], function (priv) {
if (--countdown > 0) return;
if (factory) {
try {
- exported = factory.call(global, resId);
+ exported = factory.call(global, resId, deps);
}
catch (ex) {
reject(new Error('Factory for legacy ' + resId + ' failed: ' + ex.message));
View
20 test/legacy.html
@@ -5,6 +5,8 @@
<script>
+ var curl, factoryId, factoryDeps;
+
curl = {
debug: true,
paths: {
@@ -26,7 +28,11 @@
main: 'plain_old_2',
config: {
loader: 'curl/loader/legacy',
- factory: function () { return testDomain.awesome; },
+ factory: function (id, deps) {
+ factoryId = id;
+ factoryDeps = deps;
+ return testDomain.awesome;
+ },
requires: ['test1']
}
}
@@ -51,15 +57,19 @@
'usingRequire'
]
).then(
- function (three, saucer, foobar,definedUpTop, usingRequire) {
+ function (three, saucer, foobar, definedUpTop, usingRequire) {
write('A module with dependencies loaded successfully if 3 == ' + three + '.');
write('If the following line says something then the plain js files loaded in the right order:');
//write(testDomain.foo.bar + ' ' + testDomain.awesome.sauce);
write('Did js file with no deps return a value? ' + (foobar == 'whizzah!' ? 'SUCCESS' : 'FAILED'));
-
- write('Did module with non-amd deps load dep correctly? ' + (definedUpTop == 'whizzah!' ? 'SUCCESS' : 'FAILED'));
- write('Did module with non-amd deps load dep correctly? ' + (usingRequire == 'whizzah!' ? 'SUCCESS' : 'FAILED'));
+
+ write('Did module with non-amd deps load deps correctly? ' + (definedUpTop == 'whizzah!' ? 'SUCCESS' : 'FAILED'));
+ write('Did module with non-amd deps load deps correctly? ' + (usingRequire == 'whizzah!' ? 'SUCCESS' : 'FAILED'));
+console.log(definedUpTop, usingRequire);
+ // test factory
+ write('Did legacy script with factory pass id? ' + (factoryId === 'test2/plain_old_2' ? 'SUCCESS' : 'FAILED'));
+ write('Did legacy script with factory pass dependencies? ' + (factoryDeps[0] === testDomain.foo.bar ? 'SUCCESS' : 'FAILED'));
}
);
Please sign in to comment.
Something went wrong with that request. Please try again.