Created
November 4, 2013 16:37
-
-
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.
This file contains hidden or 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
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