I successfully completed the task
within the View initialize
function
Triggering the necessary action
this.reloadCustomPages();
Below is the code for its implementation
reloadCustomPages: function(options) {
var self = this;
self.block();
self.customPages.fetch({data: $.param(options)}).always(function () {
self.unblock();
});
}
In the backend (using Java Spring), I made changes to the API to accommodate new query strings
@RequestParam(value = "pageNumber", defaultValue = "1"),
@RequestParam(value = "perPage", defaultValue = "10")
Instead of directly returning the list, it now includes pagination information like
- Total number of items in the database
- Current page number
- Page size
- And the items themselves
Here's a snippet from the server-side code for reference:
@RequestMapping(value = "/editor/pages", method = RequestMethod.GET)
public void listPages(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
@RequestParam(value = "perPage", defaultValue = "10") Integer perPage,
HttpServletResponse httpResp) throws IOException {
Long recordsTotal = pageSvc.findTotalNumberOfPages();// select count(*) from table_name
List<PbCustomPage> pages = pageSvc.findPages(pageNumber, perPage);// server side query that gets pagenated data
BackbonePaginatorResponse response = new BackbonePaginatorResponse();// I created this simple class
response.setTotalCount(recordsTotal);
response.setPageNumber(pageNumber);
response.setPerPage(perPage);
response.setItems(pages);
httpResp.setContentType("application/json");
json.createCustomPageSerializer().addProperties().serialize(response, httpResp.getWriter());// just make your server send that object
}
The function call instructs the server to fetch page 1 with a page size of 10
In my template, I have included this piece of code
<div class="pagination clear" style="text-align: center;">
<%= customPages.paginationHtml %>
</div>
This is how I populate it
customPages.paginationHtml = this.generatePagination(customPages);
Here's the crucial part:
generatePagination: function (paginationResponse) {
var currentPage = paginationResponse.pageNumber,
lastPage = paginationResponse.totalCount==0?1:Math.ceil(paginationResponse.totalCount/paginationResponse.perPage);
var html = '<ul class="pagination">';
html += '<li class="'+(currentPage == 1?"disabled":"")+'" data-pb-page-number="1"><a href="#" class="first">««</a></li>';
html += '<li class="'+(currentPage == 1?"disabled":"")+'" data-pb-page-number="'+(currentPage==1?1:currentPage-1)+'"><a href="#" class="prev">«</a></li>';
for (var i = 1; i <= lastPage; i++) {
html += '<li class="'+(currentPage == i?"active":"")+'" data-pb-page-number="'+i+'"><a href="#" class="page">'+ i +'</a></li>';
}
html += '<li class="'+(lastPage == currentPage?"disabled":"")+'" data-pb-page-number="'+(currentPage==lastPage?lastPage:currentPage+1)+'"><a href="#" class="next">»</a></li>';
html += '<li class="'+(lastPage == currentPage?"disabled":"")+'" data-pb-page-number="'+(lastPage)+'"><a href="#" class="last">»»</a></li>';
html += '</ul>';
return html;
},
Each list item has a data-pb-page-number
attribute for later use
To make requests to other pages:
Within the View initialize
function
this.el.on('click', 'ul.pagination li:not(.disabled,.active)', this.getCustomPagesPagination);
Here's the code for its implementation
getCustomPagesPagination: function (e) {
e.preventDefault();
var $el = $(e.target).closest("li");
this.reloadCustomPages({pageNumber:$el.data("pb-page-number")})
},
The resulting layout will resemble this:
https://i.sstatic.net/2hsJ6.png
This explains how I resolved my issue, although the initial question remains unanswered