Created
March 15, 2011 20:40
-
-
Save SlexAxton/871434 to your computer and use it in GitHub Desktop.
For yepnope. don't reexecute dupes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ); |
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
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?