Created
November 18, 2010 22:04
-
-
Save zerowidth/705733 to your computer and use it in GitHub Desktop.
Revisions
-
zerowidth revised this gist
Nov 18, 2010 . 2 changed files with 30 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ PaginatedCollection = Backbone.Collection.extend({ initialize: function() { _.bindAll(this, 'parse', 'url', 'pageInfo', 'nextPage', 'previousPage'); this.page = 1; }, fetch: function(options) { @@ -25,6 +25,34 @@ PaginatedCollection = Backbone.Collection.extend({ url: function() { return this.base_url + '?' + $.param({page: this.page}); }, pageInfo: function() { var info = { total: this.total, page: this.page, perPage: this.perPage, pages: Math.ceil(this.total / this.perPage), prev: false, next: false }; var max = Math.min(this.total, this.page * this.perPage); if (this.total == this.pages * this.perPage) { max = this.total; } info.range = [(this.page - 1) * this.perPage + 1, max]; if (this.page > 1) { info.prev = this.page - 1; } if (this.page < info.pages) { info.next = this.page + 1; } return info; }, nextPage: function() { this.page = this.page + 1; this.fetch(); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,29 +8,7 @@ PaginatedView = Backbone.View.extend({ 'click a.next': 'next' }, render: function() { this.el.html(app.templates.pagination(this.collection.pageInfo())); }, previous: function() { -
zerowidth revised this gist
Nov 18, 2010 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ <% if(pages > 1) { %> <% if(prev) { %> <a href="#" class="prev">previous</a> <% } else { %> <span>previous</span> <% } %> <%= range[0] %>..<%= range[1] %> of <%= total %> <% if(next) { %> <a href="#" class="next">next</a> <% } else { %> <span>next</span> <% } %> <% } %> -
zerowidth created this gist
Nov 18, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ // includes bindings for fetching/fetched PaginatedCollection = Backbone.Collection.extend({ initialize: function() { _.bindAll(this, 'parse', 'url', 'nextPage', 'previousPage'); this.page = 1; }, fetch: function(options) { options || (options = {}); this.trigger("fetching"); var self = this; var success = options.success; options.success = function(resp) { self.trigger("fetched"); if(success) { success(self, resp); } }; Backbone.Collection.prototype.fetch.call(this, options); }, parse: function(resp) { this.page = resp.page; this.perPage = resp.per_page; this.total = resp.total; return resp.models; }, url: function() { return this.base_url + '?' + $.param({page: this.page}); }, nextPage: function() { this.page = this.page + 1; this.fetch(); }, previousPage: function() { this.page = this.page - 1; this.fetch(); } }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ PaginatedView = Backbone.View.extend({ initialize: function() { _.bindAll(this, 'get_previous', 'get_next', 'render'); this.collection.bind('refresh', this.render); }, events: { 'click a.prev': 'previous', 'click a.next': 'next' }, render: function() { var page_info = {}; page_info.total = this.collection.total; page_info.page = this.collection.page; page_info.perPage = this.collection.perPage; page_info.pages = Math.ceil(page_info.total / page_info.perPage); var max = Math.min(page_info.total, page_info.page * page_info.perPage); if(page_info.total == page_info.pages * page_info.perPage) { max = page_info.total; } page_info.range = [(page_info.page - 1) * page_info.perPage + 1, max]; if(page_info.page > 1) { page_info.prev = page_info.page - 1; } else { page_info.prev = false; } if(page_info.page < page_info.pages) { page_info.next = page_info.page + 1; } else { page_info.next = false; } this.el.html(JST.pagination(page_info)); }, previous: function() { this.collection.previousPage(); return false; }, next: function() { this.collection.nextPage(); return false; } });