Permalink
Browse files

Pass dependencies through to legacy factory. Fixes #270.

  • Loading branch information...
1 parent 65dc25b commit 662138c98041249c94542c3e41c18c1dd4bb4476 @unscriptable unscriptable committed Jun 16, 2014
Showing with 35 additions and 17 deletions.
  1. +20 −12 src/curl/loader/legacy.js
  2. +15 −5 test/legacy.html
View
@@ -95,15 +95,17 @@ 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;
if (!exports && !factory) {
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,15 +121,15 @@ 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
// hasAsyncFalse, deps: getDeps+load | _export
// !hasAsyncFalse, nodeps: load | _export
// !hasAsyncFalse, deps: getDeps+load | reload | _export
- if (deps.length) {
+ if (depIds.length) {
countdown = 2;
getDeps();
load();
@@ -138,28 +140,34 @@ 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 () {
// load script, possibly with a fake mimetype
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 () {
var exported;
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
@@ -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'));
}
);

0 comments on commit 662138c

Please sign in to comment.