define(['jquery'], function($){
var PagerView = Backbone.View.extend({
tagName: "div",
className: "pager",
pageLinkTemplate: _.template(""),
initialize: function(options) {
if(!options) return;
if(!options.pages) return;
this.items = options.pages;
this.$items = $(this.items);
this.itemsPerPage = options.itemsPerPage || Math.round(this.$items.length/3);
this.numPages = Math.ceil(this.$items.length / this.itemsPerPage);
this.currentPage = options.currentPage || 1;
if(options.classes)
this.$el.addClass(options.classes);
},
events: {
"click .pager-link.num" : "goToPage",
"click .pager-link.first" : "goToFirst",
"click .pager-link.last" : "goToLast"
},
render: function(){
//Reset the pager and page items first in case we are refreshing the pager
this.$el.empty();
this.$items.show();
this.$items.removeAttr("data-page");
this.$items.removeClass("current-page");
this.$el.append(this.pageLinkTemplate({ url:"", num: "First", classes: "first" }));
for(var i=1; i<=this.numPages; i++){
var classes = "num";
if(this.currentPage == i) classes += " current-page"
this.$el.append(this.pageLinkTemplate({
url: "",
num: i,
classes: classes
}));
}
this.$el.append(this.pageLinkTemplate({ url:"", num: "Last", classes: "last" }));
var page = 1;
for(var i=0; i this.numPages){
this.currentPage = this.numPages;
this.goToPage(null, this.currentPage);
}
this.render();
}
});
return PagerView;
});