Skip to content

Instantly share code, notes, and snippets.

@SlexAxton
Created March 15, 2011 20:40
Show Gist options
  • Save SlexAxton/871434 to your computer and use it in GitHub Desktop.
Save SlexAxton/871434 to your computer and use it in GitHub Desktop.
For yepnope. don't reexecute dupes.
/**
* Yepnope Once Filter
*
* by Alex Sexton
* WTFPL
*
* Run this (ideally concatenated into the yepnope minified file) and any script that
* you try to include twice will automatically be skipped (but your callback will still run).
*
*/
(function ( yepnope ) {
var urlMap = {};
yepnope.addFilter(function ( resource ) {
if ( map[ resource.url ] ) {
resource.instead = function ( input, callback, chain, index, testresult ) {
if ( testresult ) {
// Determine callback, if any
if ( callback ) {
callback = ( {}.toString.call( callback ) == '[object Function]' ) ? callback : callback[ input ] || callback[ index ] || callback[ ( input.split( '/' ).pop().split( '?' )[ 0 ] ) ];
}
callback && callback( resource.origUrl, testResult, index );
resource.autoCallback && resource.autoCallback( resource.origUrl, testResult, index );
}
else {
resource.bypass = true;
}
};
}
else {
map[ resource.url ] = true;
}
return resource;
});
})( yepnope );
@SlexAxton
Copy link
Author

No you're absolutely correct. This was just a test, but that'd be a good thing to consider when I eventually make this a real addition. I appreciate the input.

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