Skip to content

Instantly share code, notes, and snippets.

@SlexAxton
Created March 15, 2011 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SlexAxton/870973 to your computer and use it in GitHub Desktop.
Save SlexAxton/870973 to your computer and use it in GitHub Desktop.
How you might avoid loading jQuery twice (until it's a prefix)
// If you always need jQuery regardless
yepnope([
{
load: 'jquery.js'
},
{
test: Modernizr.something,
nope: 's1.js',
complete: function () {
$('*').s1();
}
},
{
test: Modernizr.somethingElse,
nope: 's2.js',
complete: function () {
$('*').s2();
}
]);
// If you can avoid loading jQuery altogether if both fail...
yepnope([
{
test: Modernizr.something && Modernizr.somethingElse,
nope: 'jquery.js'
},
{
test: Modernizr.something,
nope: 's1.js',
complete: function () {
$('*').s1();
}
},
{
test: Modernizr.somethingElse,
nope: 's2.js',
complete: function () {
$('*').s2();
}
]);
// But soon, we'll do a 'once' prefix, and you can do it like you tried before
// First include the prefix (concatenate it into the yepnope.js file)
yepnope([
{
test: Modernizr.something,
nope: ['once!jquery.js', 's1.js'],
complete: function () {
$('*').s1();
}
},
{
test: Modernizr.somethingElse,
nope: [ 'once!jquery.js', 's2.js'],
complete: function () {
$('*').s2();
}
]);
// Or if you live more dangerously, we could also have a 'once' filter (which would just look for the same url twice, and skip it if it happens again)
// First include the yepnope once filter (concatenate it into the yepnope.js file)
yepnope([
{
test: Modernizr.something,
nope: ['jquery.js', 's1.js'],
complete: function () {
$('*').s1();
}
},
{
test: Modernizr.somethingElse,
nope: [ 'jquery.js', 's2.js'],
complete: function () {
$('*').s2();
}
]);
@unscriptable
Copy link

Hey Alex,
I like the last case better than the second-to-last for the simple reason that it's probably an edge case that they would want to load the same file twice. Why not make it remember previously downloaded files by default? You could then create an "amnesia" prefix to allow a dev to instruct yepnope to forget it has already fetched a file.
Or am I missing some common use cases?

@SlexAxton
Copy link
Author

I think my only aversion to adding in the duplication stuff is filesize. I was trying to keep only the most common cases supported by default, and then wanted to build a robust set of additions so the loader could work the way you want it. However, I could probably do this one thing with relatively little amount of code. I'll look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment