var PanigatorView = Backbone.View.extend({
el: '.pager',
events: {
'click .pageA': 'changePage'
},
template: _.template($('#paginator').html()),
initialize: function () {
var self = this;
self.setTotalPage();
self.model.bind('change', function () {
self.setTotalPage();
});
},
changePage: function (ev) {
if ($(ev.currentTarget).hasClass('disable')||$(ev.currentTarget).hasClass('pageActive')){
return false;
}
this.model.attributes.pageChange($(ev.currentTarget).data('index'))
},
render: function (page) {
this.$el.html(this.template(this.model.toJSON()));
},
setTotalPage: function () {
var showList = [];
var totalPage = Math.floor((this.model.get('total') - 1) / this.model.get('size')) + 1;
var currentPage = this.model.get('currentPage');
var showPageA = this.model.get('showPageA');
if (totalPage <= showPageA || currentPage < Math.floor(showPageA / 2) + 1) {
for (var i = 1; i <= totalPage && showList.length <= showPageA - 1; i++) {
showList.push(i)
}
} else if (currentPage > totalPage - Math.ceil(showPageA / 2)) {
for (var i = totalPage; showList.length <= showPageA - 1; i--) {
showList.unshift(i)
}
} else {
for (var i = currentPage - Math.floor(showPageA / 2); i <= currentPage + Math.ceil(showPageA / 2) - 1 && showList.length <= showPageA - 1; i++) {
showList.push(i)
}
}
this.model.set('totalPage', totalPage);
this.model.set('showList', showList);
}
});