Skip to content

Instantly share code, notes, and snippets.

@pferrel
Created November 4, 2013 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pferrel/7305338 to your computer and use it in GitHub Desktop.
Save pferrel/7305338 to your computer and use it in GitHub Desktop.
Controller that fills in the template. It does an infinite scroll and so inserts data into the collection this.videos with successive VideoSearch.query() calls for new pages of data. The view data https://gist.github.com/pferrel/7304968 looks correct.
GuideControllers.controller('VideoSearchCtrl', function($scope, VideoSearchPager) {
$scope.videoSearchPager = new VideoSearchPager();
});
// constructor function to encapsulate HTTP and pagination logic
GuideControllers.factory('VideoSearchPager', function($location, VideoSearch) {
var VideoSearchPager = function() {
this.videos = [];
this.busy = false;
this.page = '1';
this.pageNum = 1;
this.q = $location.search().q
this.done = false;
};
VideoSearchPager.prototype.nextPage = function() {
if (this.busy || this.done ) return;
this.busy = true;
VideoSearch.query({namespace: 'api', resource: 'videos', action: 'search', q: this.q, page: this.page }, function(data) {
if(data.length != 0){
for (var i = 0; i < data.length; i++) {
this.videos.push(data[i]);
}
this.pageNum = this.pageNum + 1;
this.page = (this.pageNum).toString();
this.busy = false;
} else {
this.done = true;
}
}.bind(this));
};
return VideoSearchPager;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment