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 );
@3b0b
Copy link

3b0b commented Apr 13, 2011

It looks to me as if this will execute the callback immediately if the resource has already been queued, even if it hasn't already run. I was working on a similar feature but I thought the callback needed to be passed to chain.load instead of being executed directly by the instead function. Did I misunderstand when instead is called?

@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