Refactored, to increase coverage, and remove duplicate code paths.
Showing
1 changed file
with
8 additions
and
18 deletions
... | @@ -27,11 +27,18 @@ define(function(require) { | ... | @@ -27,11 +27,18 @@ define(function(require) { |
27 | var currentPage = parseInt(this.get('currentPage')); | 27 | var currentPage = parseInt(this.get('currentPage')); |
28 | var pageSize = parseInt(this.get('pageSize')); | 28 | var pageSize = parseInt(this.get('pageSize')); |
29 | var totalCount = parseInt(this.get('totalCount')); | 29 | var totalCount = parseInt(this.get('totalCount')); |
30 | if (!currentPage || !pageSize || !totalCount) { | 30 | if (!totalCount) { |
31 | return; | 31 | return; |
32 | } | 32 | } |
33 | var pages = []; | 33 | var pages = []; |
34 | var totalPages = Math.floor((totalCount + pageSize - 1) / pageSize); | 34 | var totalPages = Math.floor((totalCount + pageSize - 1) / pageSize); |
35 | if (currentPage < 0) { | ||
36 | this.set('currentPage', 1); | ||
37 | return; | ||
38 | } else if (currentPage > totalPages) { | ||
39 | this.set('currentPage', totalPages); | ||
40 | return; | ||
41 | } | ||
35 | function addPage(self, i) { | 42 | function addPage(self, i) { |
36 | pages.push({ | 43 | pages.push({ |
37 | current: i === currentPage, | 44 | current: i === currentPage, |
... | @@ -59,10 +66,8 @@ define(function(require) { | ... | @@ -59,10 +66,8 @@ define(function(require) { |
59 | } | 66 | } |
60 | 67 | ||
61 | for (var i = startAt; i < endAt; i++) { | 68 | for (var i = startAt; i < endAt; i++) { |
62 | if (i > 0 && i <= totalPages) { | ||
63 | addPage(this, i); | 69 | addPage(this, i); |
64 | } | 70 | } |
65 | } | ||
66 | var hasPrevious = currentPage > 1; | 71 | var hasPrevious = currentPage > 1; |
67 | var hasNext = currentPage < totalPages; | 72 | var hasNext = currentPage < totalPages; |
68 | var pageJump = this.get('pageJump'); | 73 | var pageJump = this.get('pageJump'); |
... | @@ -89,21 +94,6 @@ define(function(require) { | ... | @@ -89,21 +94,6 @@ define(function(require) { |
89 | totalPages: totalPages, | 94 | totalPages: totalPages, |
90 | }); | 95 | }); |
91 | }; | 96 | }; |
92 | this.on('change:pageSize change:currentPage change:totalCount', function() { | ||
93 | var currentPage = parseInt(this.get('currentPage')); | ||
94 | var pageSize = parseInt(this.get('pageSize')); | ||
95 | var totalCount = parseInt(this.get('totalCount')); | ||
96 | if (!currentPage || !pageSize || !totalCount) { | ||
97 | return; | ||
98 | } | ||
99 | var pages = []; | ||
100 | var totalPages = Math.floor((totalCount + pageSize - 1) / pageSize); | ||
101 | if (currentPage < 0) { | ||
102 | this.set('currentPage', 1); | ||
103 | } else if (currentPage > totalPages) { | ||
104 | this.set('currentPage', totalPages); | ||
105 | } | ||
106 | }, this); | ||
107 | this.on('change:pageSize change:currentPage change:totalCount', buildPages, this); | 97 | this.on('change:pageSize change:currentPage change:totalCount', buildPages, this); |
108 | var installActions = _.bind(function installActions(eventName, nextName, hasNextName, previousName, hasPreviousName, pageCount) { | 98 | var installActions = _.bind(function installActions(eventName, nextName, hasNextName, previousName, hasPreviousName, pageCount) { |
109 | this.on(eventName, function() { | 99 | this.on(eventName, function() { | ... | ... |
-
Please register or sign in to post a comment