Skip to content

Commit

Permalink
Fix for updating pagination counts
Browse files Browse the repository at this point in the history
  • Loading branch information
dnoler committed Jan 11, 2016
1 parent 0baf272 commit b66c765
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ <h6>{{entry.company}}</h6>
limit: 10, // Number of entries to grab per page
offset: 0, // API offset, typically should be set to 0
maxCached: 20, // Max number of item to store in memory. If exceeded, entries will be offloaded
resourceKey: 'data.scrollPaginationItems', // Name of resource in API response
responseKey: 'data', // Key to API response data
resourceId: 'scrollPaginationItems', // Name of resource in API response
entryIdAttribute: '_id', // Unique identifier for an entry, should be used as the HTML id for the entry container in HTML
loadMoreText: 'Load more customers', // Text for pagination buttons
apiParams: { // Any additional parameters to send to the API
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
limit: 10, // Number of entries to grab per page
offset: 0, // API offset, typically should be set to 0
maxCached: 20, // Max number of item to store in memory. If exceeded, entries will be offloaded
resourceKey: 'data.scrollPaginationItems', // Name of resource in API response
responseKey: 'data', // Key to API response data
resourceId: 'scrollPaginationItems', // Name of resource in API response
entryIdAttribute: '_id', // Unique identifier for an entry, should be used as the HTML id for the entry container in HTML
loadMoreText: 'Load more customers', // Text for pagination buttons
apiParams: { // Any additional parameters to send to the API
Expand Down
13 changes: 8 additions & 5 deletions lib/ui/scroll-pagination/scroll-pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@
var params = {};
_.extend(params, $scope._options.apiParams, {limit: $scope._options.limit, offset: $scope._options.offset});
$scope.apiResource.query({params: params}).then(function(response) {
self.addEntries(self.getEntries(response), prepend);
self.updateButtonVisibilityFlags(response.data);
var responseData = self.getResponseData(response);
if (responseData && responseData[$scope._options.resourceId]) {
self.addEntries(responseData[$scope._options.resourceId], prepend);
}
self.updateButtonVisibilityFlags(responseData);
block.stop();
}, function() {
$log.error('API call failed');
block.stop();
});
};

this.getEntries = function(data) {
if (data && $scope._options.resourceKey) {
var keys = $scope._options.resourceKey.split('.');
this.getResponseData = function(data) {
if (data && $scope._options.responseKey) {
var keys = $scope._options.responseKey.split('.');
var nestedData = data;
var noData = false;
_.each(keys, function(key) {
Expand Down
26 changes: 14 additions & 12 deletions lib/ui/scroll-pagination/tests/scroll-pagination-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('avScrollPagination', function() {
}};
scope = {};
scope.entries = [];
scope.options = { resourceKey: 'data.entries' };
scope.options = { responseKey: 'data', resourceId: 'entries' };
scope.apiResource = {
query: function() {
return {
Expand All @@ -43,7 +43,8 @@ describe('avScrollPagination', function() {

it('sets options', inject(function(AV_SCROLL_PAGINATION) {
expect(scope._options).toBeDefined();
expect(scope._options.resourceKey).toBe('data.entries');
expect(scope._options.responseKey).toBe('data');
expect(scope._options.resourceId).toBe('entries');
expect(scope._options.limit).toBe(AV_SCROLL_PAGINATION.DEFAULT_OPTIONS.limit);
expect(scope._options.offset).toBe(AV_SCROLL_PAGINATION.DEFAULT_OPTIONS.offset);
expect(scope._options.maxCached).toBe(AV_SCROLL_PAGINATION.DEFAULT_OPTIONS.maxCached);
Expand Down Expand Up @@ -133,32 +134,33 @@ describe('avScrollPagination', function() {
});
});

describe('getEntries', function() {
describe('getResponseData', function() {
it('returns an empty array if no data exists', function() {
var result = controller.getEntries();
var result = controller.getResponseData();
expect(result).toBeDefined();
expect(_.isArray(result)).toBe(true);
expect(result.length).toBe(0);
});
it('returns an empty array if no resource key is specified', function() {
delete scope._options.resourceKey;
var result = controller.getEntries(response);
it('returns an empty array if no response key is specified', function() {
delete scope._options.responseKey;
var result = controller.getResponseData(response);
expect(result).toBeDefined();
expect(_.isArray(result)).toBe(true);
expect(result.length).toBe(0);
});
it('returns an empty array if key not found', function() {
scope._options.resourceKey = 'data.stuff';
var result = controller.getEntries(response);
scope._options.responseKey = 'data.stuff';
var result = controller.getResponseData(response);
expect(result).toBeDefined();
expect(_.isArray(result)).toBe(true);
expect(result.length).toBe(0);
});
it('returns data', function() {
var result = controller.getEntries(response);
var result = controller.getResponseData(response);
expect(result).toBeDefined();
expect(_.isArray(result)).toBe(true);
expect(result.length).toBe(4);
expect(result.entries).toBeDefined();
expect(_.isArray(result.entries)).toBe(true);
expect(result.entries.length).toBe(4);
});
});

Expand Down

0 comments on commit b66c765

Please sign in to comment.