From b66c765db4d9865580b0e83be232fdac5aa679d1 Mon Sep 17 00:00:00 2001 From: Danny Noler Date: Mon, 11 Jan 2016 14:59:01 -0500 Subject: [PATCH] Fix for updating pagination counts --- .../docs/scroll-pagination-ui-demo.html | 3 ++- .../docs/scroll-pagination-ui-demo.js | 3 ++- lib/ui/scroll-pagination/scroll-pagination.js | 13 ++++++---- .../tests/scroll-pagination-spec.js | 26 ++++++++++--------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.html b/lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.html index fb46c0da..4d05a543 100644 --- a/lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.html +++ b/lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.html @@ -61,7 +61,8 @@
{{entry.company}}
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 diff --git a/lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.js b/lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.js index 662cc317..d8db21f7 100644 --- a/lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.js +++ b/lib/ui/scroll-pagination/docs/scroll-pagination-ui-demo.js @@ -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 diff --git a/lib/ui/scroll-pagination/scroll-pagination.js b/lib/ui/scroll-pagination/scroll-pagination.js index 01b0827d..6ae34005 100644 --- a/lib/ui/scroll-pagination/scroll-pagination.js +++ b/lib/ui/scroll-pagination/scroll-pagination.js @@ -36,8 +36,11 @@ 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'); @@ -45,9 +48,9 @@ }); }; - 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) { diff --git a/lib/ui/scroll-pagination/tests/scroll-pagination-spec.js b/lib/ui/scroll-pagination/tests/scroll-pagination-spec.js index 79937c4f..d3bfc631 100644 --- a/lib/ui/scroll-pagination/tests/scroll-pagination-spec.js +++ b/lib/ui/scroll-pagination/tests/scroll-pagination-spec.js @@ -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 { @@ -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); @@ -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); }); });