community-app/app/scripts/services/Paginator.js
2013-09-13 10:49:03 +05:30

47 lines
1.5 KiB
JavaScript

(function(module) {
mifosX.services = _.extend(module, {
PaginatorService: function(scope, httpService) {
this.paginate = function(fetchFunction, pageSize) {
var paginator = {
hasNextVar: false,
next: function() {
if (this.hasNextVar) {
this.currentOffset += pageSize;
this._load();
}
},
_load: function() {
var self = this;
fetchFunction(this.currentOffset, pageSize + 1, function(items) {
self.currentPageItems = items.pageItems;
self.hasNextVar = items.pageItems.length === pageSize + 1;;
});
},
hasNext: function() {
return this.hasNextVar;
},
previous: function() {
if(this.hasPrevious()) {
this.currentOffset -= pageSize;
this._load();
}
},
hasPrevious: function() {
return this.currentOffset !== 0;
},
currentPageItems: [],
currentOffset: 0
};
// Load the first page
paginator._load();
return paginator;
};
}
});
mifosX.ng.services.service('PaginatorService', ['$rootScope', 'HttpService', mifosX.services.PaginatorService]).run(function($log) {
$log.info("PaginatorService initialized");
});
}(mifosX.services || {}));