Created
September 7, 2013 21:17
-
-
Save gabrielhpugliese/6479387 to your computer and use it in GitHub Desktop.
Snippet for using Google Youtube APIs with Meteor rendering engine (not Meteor UI). Assume the template is called "channel". You only need to implement onPlayerReady of line 44. It uses jQuery.getScript();
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
Meteor.startup(function () { | |
Session.set('YTApiReady', false); | |
Session.set('channelRendered', false); | |
}); | |
onYouTubeIframeAPIReady = function() { | |
Session.set('YTApiReady', true); | |
}; | |
Template.channel.created = function () { | |
if (typeof player === 'undefined') | |
$.getScript('https://www.youtube.com/iframe_api', function () {}); | |
}; | |
Template.channel.rendered = function() { | |
Session.set('channelRendered', true); | |
}; | |
Template.channel.destroyed = function () { | |
Session.set('channelRendered', false); | |
}; | |
Deps.autorun(function (c) { | |
if (Session.equals('YTApiReady', false) || | |
Session.equals('channelRendered', false)) { | |
return; | |
} | |
var interval = Meteor.setInterval(function () { | |
if(!document.getElementById('player-div')) { | |
return; | |
} | |
var playerDiv = document.createElement('div'), | |
channel = Template.channel.getChannel(); | |
playerDiv.id = 'player-div'; | |
document.getElementById('player-parent').innerHTML = ''; | |
document.getElementById('player-parent').appendChild(playerDiv); | |
player = null; | |
player = new YT.Player('player-div', { | |
videoId: channel.URL, | |
events: { | |
'onReady': onPlayerReady | |
} | |
}); | |
Meteor.clearInterval(interval); | |
}, 100); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have no idea how much time you just saved me. Thanks!