Skip to content

Instantly share code, notes, and snippets.

@leite
Created December 23, 2011 20:11
Show Gist options
  • Save leite/1515241 to your computer and use it in GitHub Desktop.
Save leite/1515241 to your computer and use it in GitHub Desktop.
[JQuery Plugin] load a list of videos from youtube
/*
* JQuery Videos Loader
* @xxleite ( 23/12/2011 17:45:00 -3 UTC )
*
* parameters:
* user = youtube username
* maxVideos = total of videos to load
* onSuccess = function callback to receive videos array
*
*/
jQuery.fn.loadYoutubeVideos = function( opts ) {
//
this.opts = $.extend( {
youtubeMaxResults : 50,
user : '',
maxVideos : 0,
lastVideosCount : 1,
videosList : [],
onSuccess : null
}, opts );
// ajax callback and logic of pagin'
this.onAjaxSuccess = function (data) {
this.opts.videosList = this.opts.videosList.concat( data.feed.entry );
this.opts.lastVideosCount += this.opts.youtubeMaxResults;
// we need a callback please!
if( typeof this.opts.onSuccess != 'function' ) { return; }
if ( this.opts.lastVideosCount >= this.opts.maxVideos ) {
this.opts.onSuccess( this.opts.videosList );
} else {
this.loadVideos();
}
};
// ajax load
this.loadVideos = function( ) {
jQuery.ajax({
url: 'http://gdata.youtube.com/feeds/api/users/' + this.opts.user + '/uploads/',
dataType: 'jsonp',
context: this,
data: {
v : 2,
alt : 'json',
'max-results' : this.opts.youtubeMaxResults,
'start-index' : this.opts.lastVideosCount
},
success : this.onAjaxSuccess
});
};
// call main ajax handler
this.loadVideos();
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment