One nice feature of require.js is that it supports "lazy loading"
libraries. That is, it can avoid loading libraries that are not needed.
Here's my own use-case for it, and how I think it might be designed and
implemented here.
We use both jQuery and jQuery-UI on our site, and jQuery UI is large
enough that we would prefer not to load it on pages where we don't need
it.
With the current design, I could address this by creating two small
scripts:
# Set up definition to load jQuery with fall back
load-jquery.js
# Set up definition to load jQuery with fall back
load-jquery-and-jquery-ui.js
I would then load whichever one I needed, and find-and-replace my jQuery
"ready" commands to this instead:
fallback.ready(function() {
// Completed
});
The way I would rather use fallback.js to put all my CDN and fallback
references in one file, and then I would just declare which ones I
needed on a given page, and just those would be loaded.
Here's how that might look:
# My local script which calls fallback.load()
cdn-fallbacks.js
Then, I would replace the jQuery ready() calls depending on which ones I
wanted to load:
# Load everything by default (current behavior)
fallback.ready(function() {
// Completed
});
# explicitly load just jQuery
fallback.ready(['jQuery'],function() {
// Completed
});
# explicitly load just jQuery and jQuery-UI
fallback.ready(['jQuery','jQuery-UI'],function() {
// Completed
});
In the implementation, the call to "this.initialize();" would be moved
from "load()" to "ready()" and would take as argument the optional array
that can now be passed to ready(). If the list has entries in it, only
those elements would be loaded. The call to initialize(); run
immediately when ready() is called (after the callbacks are setup).
require.js supports basically the same feature with the same syntax, and
I assume it's implemented in a similar way.
Without this feature, I could end up with a proliferation of files
related to fallback.js, that each load different combinations of
modules. Or more likely, I might consider switchng to using require.js
rather than deal with the lack of missing feature.
Thanks for your consideration.