Skip to content

Instantly share code, notes, and snippets.

@adamf321
Created April 7, 2016 23:18
Show Gist options
  • Save adamf321/34d7ccc75b83f3ad939141606444bb31 to your computer and use it in GitHub Desktop.
Save adamf321/34d7ccc75b83f3ad939141606444bb31 to your computer and use it in GitHub Desktop.
angular
.module('app')
.factory('paginationService', paginationService);
paginationService.$inject = ['lnCmsClientService', '$state'];
function paginationService(lnCmsClientService, $state){
var current = 1;
var total = 1;
var callback = () => {};
const endpoint = $state.current.data.endpoint;
return {
init: init,
nextPage: nextPage,
hasPages: hasPages
};
function init( data = {} ) {
total = data.pages || 1;
current = 1;
}
function nextPage(cb){
if ( hasPages() ) {
var params = angular.extend(
{},
$state.current.data.fixedParams,
{ paged: current + 1 }
);
callback = cb;
lnCmsClientService
.getView(endpoint, params)
.then(response);
}
}
function response(response) {
current += 1;
callback(response);
}
function hasPages(){
return current < total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment