Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit b669970

Browse files
committed
Create Paginator.js
1 parent 5108ae7 commit b669970

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

modules/Paginator.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
angular.module('app').factory('Paginator', function($http){
2+
3+
var Paginator = function(url, options){
4+
this.url = url;
5+
this.options = {
6+
limit: 50,
7+
offset: 0
8+
};
9+
//add everything to the this.params object
10+
for (var name in options){
11+
this.options[name] = options[name];
12+
}
13+
this.items = [];
14+
this.hasMore = true;
15+
this.loading = false;
16+
//make first call for items at initialization
17+
this.next();
18+
};
19+
20+
Paginator.prototype.next = function(){
21+
var self = this;
22+
23+
this.loading = true;
24+
25+
if (this.hasMore) {
26+
return $http.get(this.url, { params: this.options }).then(function(response) {
27+
//If the results are less than the required limit then the results are finished
28+
if(response.data.results.length < self.options.limit){
29+
self.hasMore = false
30+
}
31+
self.items = self.items.concat(response.data.results);
32+
self.options.offset = self.items.length;
33+
self.loading = false;
34+
return self.items;
35+
});
36+
}
37+
}
38+
39+
return Paginator;
40+
});

0 commit comments

Comments
 (0)