Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched to browserify / grunt built process
Now bringing in `backbone-collection-proxy` to deal with the tricky parts of proxy-ing the paginated collection through the returned object.
- Loading branch information
Showing
5 changed files
with
277 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module.exports = function (grunt) { | ||
grunt.initConfig({ | ||
|
||
browserify: { | ||
basic: { | ||
src: [], | ||
dest: './backbone-paginated-collection.js', | ||
options: { | ||
external: [ 'underscore', 'backbone' ], | ||
alias: ['./index.js:backbone-paginated-collection'] | ||
} | ||
} | ||
}, | ||
|
||
umd: { | ||
default: { | ||
src: './backbone-paginated-collection.js', | ||
template: './templates/umd.hbs', | ||
objectToExport: "require('backbone-paginated-collection')", | ||
globalAlias: 'PaginatedCollection', | ||
deps: { | ||
'default': ['_', 'Backbone'], | ||
amd: ['underscore', 'backbone'], | ||
cjs: ['underscore', 'backbone'], | ||
global: ['_', 'Backbone'] | ||
}, | ||
browserifyMapping: '{"backbone":Backbone,"underscore":_}' | ||
} | ||
} | ||
|
||
}); | ||
|
||
grunt.loadNpmTasks('grunt-browserify'); | ||
grunt.loadNpmTasks('grunt-umd'); | ||
|
||
grunt.registerTask('default', ['browserify', 'umd']); | ||
}; |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
|
||
var _ = require('underscore'); | ||
var Backbone = require('backbone'); | ||
var proxyCollection = require('backbone-collection-proxy'); | ||
|
||
function updatePagination() { | ||
var start = this.getPage() * this.getPerPage(); | ||
var end = start + this.getPerPage(); | ||
|
||
this._collection.reset(this.superset().toArray().slice(start, end)); | ||
} | ||
|
||
function recalculatePagination() { | ||
var length = this.superset().length; | ||
var perPage = this.getPerPage(); | ||
|
||
// If the # of objects can be exactly divided by the number | ||
// of pages, it would leave an empty last page if we took | ||
// the floor. | ||
var totalPages = length % perPage === 0 ? | ||
(length / perPage) : Math.floor(length / perPage) + 1; | ||
|
||
this._totalPages = totalPages; | ||
|
||
// If the current page no longer exists, switch to the last | ||
// existing page in the set. | ||
if (this.getPage() >= totalPages) { | ||
this.setPage(totalPages - 1); | ||
} else { | ||
updatePagination.call(this); | ||
} | ||
} | ||
|
||
function Paginated(superset, options) { | ||
// Save a reference to the original collection | ||
this._superset = superset; | ||
|
||
// The idea is to keep an internal backbone collection with the paginated | ||
// set, and expose limited functionality. | ||
this._collection = new Backbone.Collection(superset.toArray()); | ||
this.setPerPage(options ? options.perPage: this._defaultPerPage); | ||
|
||
proxyCollection(this._collection, this); | ||
|
||
this.listenTo(this._superset, 'add', recalculatePagination); | ||
this.listenTo(this._superset, 'remove', recalculatePagination); | ||
this.listenTo(this._superset, 'reset', recalculatePagination); | ||
} | ||
|
||
var methods = { | ||
|
||
_defaultPerPage: 20, | ||
|
||
setPerPage: function(perPage) { | ||
this._perPage = perPage; | ||
recalculatePagination.call(this); | ||
this.setPage(0); | ||
|
||
this.trigger('paginated:change:perPage', { | ||
perPage: perPage, | ||
numPages: this.getNumPages() | ||
}); | ||
}, | ||
|
||
setPage: function(page) { | ||
// The lowest page we could set | ||
var lowerLimit = 0; | ||
// The highest page we could set | ||
var upperLimit = this.getNumPages() - 1; | ||
|
||
// If the page is higher or lower than these limits, | ||
// set it to the limit. | ||
page = page > lowerLimit ? page : lowerLimit; | ||
page = page < upperLimit ? page : upperLimit; | ||
|
||
this._page = page; | ||
updatePagination.call(this); | ||
|
||
this.trigger('paginated:change:page', { page: page }); | ||
}, | ||
|
||
getPerPage: function() { | ||
return this._perPage; | ||
}, | ||
|
||
getNumPages: function() { | ||
return this._totalPages; | ||
}, | ||
|
||
getPage: function() { | ||
return this._page; | ||
}, | ||
|
||
hasNextPage: function() { | ||
return this.getPage() < this.getNumPages() - 1; | ||
}, | ||
|
||
hasPrevPage: function() { | ||
return this.getPage() > 0; | ||
}, | ||
|
||
nextPage: function() { | ||
this.movePage(1); | ||
}, | ||
|
||
prevPage: function() { | ||
this.movePage(-1); | ||
}, | ||
|
||
movePage: function(delta) { | ||
this.setPage(this.getPage() + delta); | ||
}, | ||
|
||
superset: function() { | ||
return this._superset; | ||
} | ||
|
||
}; | ||
|
||
// Build up the prototype | ||
_.extend(Paginated.prototype, methods, Backbone.Events); | ||
|
||
module.exports = Paginated; | ||
|
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(function(root, factory) { | ||
if(typeof exports === 'object') { | ||
module.exports = factory({{{cjsDependencies}}}); | ||
} | ||
else if(typeof define === 'function' && define.amd) { | ||
define({{#if amdModuleId}}'{{amdModuleId}}', {{/if}}[{{{amdDependencies}}}], factory); | ||
} | ||
else { | ||
root.{{globalAlias}} = factory({{{globalDependencies}}}); | ||
} | ||
}(this, function({{dependencies}}) { | ||
var require=function(name){return {{{ browserifyMapping }}}[name];}; | ||
{{{code}}} | ||
return {{{objectToExport}}}; | ||
|
||
})); |