Skip to content

Instantly share code, notes, and snippets.

@MeoMix
Created November 9, 2012 03:52
Show Gist options
  • Save MeoMix/4043589 to your computer and use it in GitHub Desktop.
Save MeoMix/4043589 to your computer and use it in GitHub Desktop.
FROM THIS:
//Provides an interface to the YouTube iFrame.
//Starts up Player object after receiving a ready response from the YouTube API.
var onReady_funcs = [], api_isReady = false;
define(['onYouTubePlayerAPIReady'],function(){
'use strict';
//This code will trigger onYouTubePlayerAPIReady
$(window).load(function(){
//Load YouTube Frame API
var youtubeIframeAPIScript = document.createElement("script");
youtubeIframeAPIScript.src = "https://www.youtube.com/iframe_api"; /* Load Player API*/
var before = document.getElementsByTagName("script")[0];
before.parentNode.insertBefore(youtubeIframeAPIScript, before);
});
// Define YT_ready function.
var isReady = (function () {
//@param func function Function to execute on ready
//@param func Boolean If true, all qeued functions are executed
//@param b_before Boolean If true, the func will added to the first position in the queue
return function (func, b_before) {
if (func === true) {
api_isReady = true;
for (var i = 0; i < onReady_funcs.length; i++) {
// Removes the first func from the array, and execute func
onReady_funcs.shift()();
}
}
else if (typeof func === "function") {
if (api_isReady){
func();
}
else{
onReady_funcs[b_before ? "unshift" : "push"](func);
}
}
};
})();
return {
ready: isReady
};
});
//TO THIS:
//Provides an interface to the YouTube iFrame.
//Starts up Player/SongValidator object after receiving a ready response from the YouTube API.
define(['onYouTubePlayerAPIReady'],function(){
'use strict';
var events = {
onApiReady: 'onApiReady'
};
//This code will trigger onYouTubePlayerAPIReady
$(window).load(function(){
$('script:first').before($('<script/>', {
src: 'https://www.youtube.com/iframe_api'
}));
});
return {
ready: function(){
$(this).trigger(events.onApiReady);
},
onApiReady: function(event){
$(this).on(events.onApiReady, event);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment