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); | |
}); |
Neil, I would expect that the getChannel() function does a lookup into the mongo collection that contains whatever metadata he's maintaining each video. I am borrowing this code too and I have a collection with the metadata for each video that I want to play. I hope that helps.
Yes, it's a channel on mongodb
You have no idea how much time you just saved me. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for posting this. It's been a HUGE help for an app I'm writing. Would you mind sharing the getChannel() function that you're using to set the videoId?