| 1 | window.MyMixins = {}; |
| 2 | MyMixins.PaginateMixin = { |
| 3 | initialize: function() { |
| 4 | this.currentPage = 0; |
| 5 | }, |
| 6 | |
| 7 | events: { |
| 8 | 'click .previous': 'goToPreviousPage', |
| 9 | 'click .next': 'goToNextPage' |
| 10 | }, |
| 11 | |
| 12 | render: function() { |
| 13 | this.$el.append('<div class="previous"></div>'); |
| 14 | this.$el.append('<div class="next"></div>'); |
| 15 | this.renderCurrentPage(this.currentPage); |
| 16 | }, |
| 17 | |
| 18 | goToPage: function(page) { |
| 19 | var numberOfPages = this.numberOfPages(); |
| 20 | |
| 21 | this.currentPage = Math.min(Math.max(page, 0), numberOfPages - 1); |
| 22 | this.renderCurrentPage(this.currentPage); |
| 23 | |
| 24 | this.$('.previous').toggleClass('hidden', this.currentPage == 0); |
| 25 | this.$('.next').toggleClass('hidden', this.currentPage == numberOfPages - 1); |
| 26 | |
| 27 | return this.currentPage; |
| 28 | }, |
| 29 | |
| 30 | goToNextPage: function() { |
| 31 | return this.goToPage(this.currentPage + 1); |
| 32 | }, |
| 33 | |
| 34 | goToPreviousPage: function() { |
| 35 | return this.goToPage(this.currentPage - 1); |
| 36 | } |
| 37 | } |