-
Notifications
You must be signed in to change notification settings - Fork 28
Support loading multiple JavaScript files before calling back #39
Comments
@coopy -- We're aiming to keep little-loader razor thin. I think this is most appropriate for documentation / best practices around "patterns" captured in: #20 Extra pattern suggestions and documentation PRs most welcome! Also, I'm open to examples (maybe in the test suite) we can add to. For you here, here's a strawman pattern for parallel loading: var load = window._lload; // or however required in...
var parallelLoad = function (libs, callback) {
var finished = 0;
var errs = [];
if (!(libs && libs.length)) { return callback([]); }
libs.forEach(function (lib) {
load(lib, function (err) {
// capture errors array and return all to callback.
// Note: _Could_ do other logic to bail early when first error occurs.
errs = errs.concat(err ? [err] : []);
finished++;
if (finished === libs.length) {
callback(errs);
}
});
});
};
parallelLoad([
"foo.js",
"bar.js"
], function (errs) {
console.log("ALL DONE!");
console.log("Errors: " + errs);
}); And, here's a strawman pattern for sequential loading: var load = window._lload; // or however required in...
var seriesLoad = function (libs, callback) {
// Load function and recursively call next function.
var loadLib = function (idx) {
var lib = libs[idx];
// Done with all libs.
if (typeof lib === "undefined") {
callback();
}
// Load this lib.
load(lib, function (err) {
// Short circuit if error.
if (err) { return callback(err); }
// Recursively go for next.
loadLib(idx + 1);
});
}
// Start at first lib index.
loadLib(0);
};
seriesLoad([
"foo.js",
"bar.js"
], function (err) {
console.log("ALL DONE!");
console.log("Error: " + err);
}); |
I'm not against adding a helper for this at all, but the problem is that it muddies the waters a bit w.r.t. the main important feature of I'm assuming you're just using |
@coopy @exogen -- I've updated by two patterns above. As you can see with even these simple scenarios, there is a lot of choice about how to handle errors / early termination / etc. I am definitely up for documenting / explaining these patterns. I'm potentially open to including them as separate includes as utilities... |
What would be particularly great is if it were possible to download several files in parallel, but execute them in order. This does not seem possible with the current library, but would greatly help bootstrapping applications that have several external dependencies |
@jknight12882: That's unfortunately not possible due to CORS – it's one reason why AMD was invented! :) |
@exogen CORS has nothing to do with it. CORS deals with making ajax calls. You can load |
You could control evaluation order by not appending the script tags to the |
@jknight12882: It does have to do with CORS, trust me. You can't get the content of scripts from other domains, which would be required for delaying their execution. |
@exogen see my comment above about controlling evaluation order by waiting til all scripts have downloaded before appending them to the DOM. |
Try it and report back my friend – this is a well understood topic! :) |
my mistake, it looks like the delayed appending to |
@jknight12882: True that, |
I'm considering using
little-loader
to dynamically load in component suites at run-time. Currently, there is only support for a singularsrc
string. I would welcome support for an array ofsrc
strings, to load in multiple libraries. Is this in alignment with the roadmap?The text was updated successfully, but these errors were encountered: