diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-09-13 16:58:38 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-09-13 16:58:38 +0200 |
commit | 08df2433cad543587458a64a0b81122e1f966cb8 (patch) | |
tree | 466125e54bba6ca99bf695a4274722c7284fa36e /js/service | |
parent | 11c4b03d70583d8b4c7e7bce408a3c3a3d9c1f17 (diff) |
autopaging
Diffstat (limited to 'js/service')
-rw-r--r-- | js/service/ItemResource.js | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/js/service/ItemResource.js b/js/service/ItemResource.js index c988e6729..432b2c8c7 100644 --- a/js/service/ItemResource.js +++ b/js/service/ItemResource.js @@ -13,12 +13,18 @@ app.factory('ItemResource', function (Resource, $http, BASE_URL, var ItemResource = function ($http, BASE_URL, ITEM_BATCH_SIZE) { Resource.call(this, $http, BASE_URL); - this.starredCount = 0; this.batchSize = ITEM_BATCH_SIZE; + this.clear(); }; ItemResource.prototype = Object.create(Resource.prototype); + ItemResource.prototype.clear = function () { + this.starredCount = 0; + this.lowestId = 0; + this.highestId = 0; + Resource.prototype.clear.call(this); + }; ItemResource.prototype.receive = function (value, channel) { switch (channel) { @@ -32,6 +38,24 @@ app.factory('ItemResource', function (Resource, $http, BASE_URL, break; default: + var self = this; + value.forEach(function (item) { + // initialize lowest and highest id + if (self.lowestId === 0) { + self.lowestId = item.id; + } + if (self.highestId === 0) { + self.highestId = item.id; + } + + if (item.id > self.highestId) { + self.highestId = item.id; + } + if (item.id < self.lowestId) { + self.lowestId = item.id; + } + }); + Resource.prototype.receive.call(this, value, channel); } }; @@ -151,15 +175,24 @@ app.factory('ItemResource', function (Resource, $http, BASE_URL, }; - ItemResource.prototype.autoPage = function (type, id) { + ItemResource.prototype.autoPage = function (type, id, oldestFirst) { + var offset; + + if (oldestFirst) { + offset = this.highestId; + } else { + offset = this.lowestId; + } + return this.http({ url: this.BASE_URL + '/items', method: 'GET', params: { type: type, id: id, - offset: this.size(), - limit: this.batchSize + offset: offset, + limit: this.batchSize, + oldestFirst: oldestFirst } }); }; |