Skip to content

Commit

Permalink
updates based on discussion in backbone-paginator#1
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Jan 23, 2012
1 parent 0830ae3 commit 48327a4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 41 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ This project seeks to provide a stable, comprehensive pagination component for B

##API

* ```Paginator.page```: The first 'page' to display in the paginated view
* ```Paginator.perPage```: The number of results to display per 'page'
* ```Paginator.queryPage```: The current page in the service being displayed (e.g /?page=1)
* ```Paginator.queryPerPage```: The number of results per query/page to request from the service
* ```Paginator.queryTotalPages```: The total number of pages available to query from the service
* ```Paginator.cParams.page```: The first 'page' to display in the paginated view
* ```Paginator.cParams.perPage```: The number of results to display per 'page'
* ```Paginator.queryParams.page```: The current page in the service being displayed (e.g /?page=1)
* ```Paginator.queryParams.perPage```: The number of results per query/page to request from the service
* ```Paginator.queryParams.totalPages```: The total number of pages available to query from the service
* ```Paginator.nextPage()```: Go to the next `page` in the paginated view
* ```Paginator.previousPage()```: Go to the previous `page` in the paginated view
* ```Paginator.goTo(page)```: Go to a specific page
* ```Paginator.setSort(key, direction)```: Sort by a specific key in a direction ('asc'/'desc')
* ```Paginator.requestNextPage()```: Request the next page of results from the service
* ```Paginator.requestPreviousPage()```: Request the previous page of results from the service

Currently in the process of extending queryParam support for sending sort and other parameters to the backend so sorting it possible there as well as client-side.

##Credits

Expand Down
2 changes: 1 addition & 1 deletion collections/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
model : model,

/*by using the &callback=? option, Backbone will switch to JSONP for us*/
url: 'http://search.twitter.com/search.json?q=batman' + '&rpp=' + pagination.queryPerPage + ' &include_entities=true&result_type=recent&callback=?',
url: 'http://search.twitter.com/search.json?q=' + pagination.queryParams.query + '&rpp=' + pagination.queryParams.perPage + ' &include_entities=true&result_type=recent&callback=?',

parse : function ( response ) {
var tags = response.results;
Expand Down
96 changes: 62 additions & 34 deletions utils/backbone.paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,67 @@
(function ( mixins ) {

mixins.Paginator = {
/** how many items to show per page in the view */
perPage : 20,

/** page to start off on for pagination in the view */
page : 1,
/*Parameters to pass back to the server*/
queryParams:{

/**current page to query from the service*/
page: 1,

/**current page to query from the service*/
queryPage: 1,
/*how many results to query from the service*/
perPage: 30,

/*how many results to query from the service*/
queryPerPage: 30,
/*maximum number of pages that can be queried from the server*/
totalPages:10,

/*sort direction*/
sortDirection: 'asc',

/*sort field*/

sortField: 'name',

/*query*/
query: 'batman'

},

/*Work against the result set retrieved so far*/
cParams:{

/** how many items to show per page in the view */
perPage : 20,

/** page to start off on for pagination in the view */
page : 1,

/*sort field*/
sortField: 'text',

/*sort direction*/
sortDirection: 'asc'

},

/*maximum number of pages that can be queried from the server*/
queryTotalPages:10,

/**
*
*/
nextPage : function () {
this.page = ++this.page;
this.cParams.page = ++this.cParams.page;
this.pager();
},

previousPage : function () {
this.page = --this.page || 1;
this.cParams.page = --this.cParams.page || 1;
this.pager();
},

goTo : function (page) {
this.page = parseInt(page,10);
this.cParams.page = parseInt(page,10);
this.pager();
},

howManyPer : function (perPage) {
this.page = 1;
this.perPage = perPage;
this.cParams.page = 1;
this.cParams.perPage = perPage;
this.pager();
},

Expand All @@ -52,8 +77,8 @@

pager : function (sort, direction) {
var self = this,
start = (self.page-1)*this.perPage,
stop = start+self.perPage;
start = (self.cParams.page-1)*this.cParams.perPage,
stop = start+self.cParams.perPage;

if (self.orgmodels === undefined) {
self.orgmodels = self.models;
Expand Down Expand Up @@ -108,30 +133,30 @@

info = {
totalRecords : totalRecords,
page : self.page,
perPage : self.perPage,
page : self.cParams.page,
perPage : self.cParams.perPage,
totalPages : totalPages,
lastPage : totalPages,
lastPagem1 : totalPages-1,
previous : false,
next : false,
page_set : [],
startRecord : (self.page - 1) * self.perPage + 1,
endRecord : Math.min(totalRecords, self.page * self.perPage)
startRecord : (self.cParams.page - 1) * self.cParams.perPage + 1,
endRecord : Math.min(totalRecords, self.cParams.page * self.cParams.perPage)
};

if (self.page > 1) {
info.prev = self.page - 1;
info.prev = self.cParams.page - 1;
}

if (self.page < info.totalPages) {
info.next = self.page + 1;
info.next = self.cParams.page + 1;
}

info.pageSet = self.setPagination(info);

info.queryTotalPages = self.queryTotalPages;
info.queryPage = self.queryPage;
info.queryTotalPages = self.queryParams.totalPages;
info.queryPage = self.queryParams.page;


self.information = info;
Expand All @@ -142,18 +167,21 @@
// of the internally managed collection

requestNextPage: function(){
if(this.queryPage >= 0){
this.queryPage += 1;
if(this.queryParams.page >= 0){
this.queryParams.page += 1;
}

this.fetch({data: {page: this.queryPage}});
//needs to be updated so any parameters we want
//to push back to the server-side are configured
//and pushed via queryParams
this.fetch({data: {page: this.queryParams.page}});
},

requestPreviousPage: function(){
if(this.queryPage >= 0){
this.queryPage -= 1;
if(this.queryParams.page >= 0){
this.queryParams.page -= 1;
}
this.fetch({data: {page: this.queryPage}});
this.fetch({data: {page: this.queryParams.page}});
},

setPagination : function (info) {
Expand Down
1 change: 0 additions & 1 deletion views/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
nextResultPage: function(e){
e.preventDefault();
this.collection.requestNextPage();

},

previousResultPage : function(e){
Expand Down

0 comments on commit 48327a4

Please sign in to comment.