Created
May 24, 2012 16:37
-
-
Save dperrymorrow/2782621 to your computer and use it in GitHub Desktop.
Backbone Pagination helper
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
Backbone.Helpers ||= {} | |
class Backbone.Helpers.Paginate extends dpm.Callbacks | |
# we simply store a ref to the instance and the property so that we dont have to store the data in two places, ! too big | |
constructor:(inst,prop,@perPage)-> | |
super() | |
@setSource inst, prop | |
@reset() | |
reset:()-> | |
@currentPage = 0 | |
@calcTotal() | |
@redraw() | |
@ | |
calcTotal:()-> | |
@totalPages = Math.ceil( @getArr().length/@perPage ) - 1 | |
@ | |
getArr:-> | |
if @inst instanceof Backbone.Model | |
return @inst.get @prop | |
else | |
return @inst[@prop] | |
setPage:(pageNum)-> | |
@currentPage = pageNum | |
@redraw() | |
@ | |
next:-> | |
@currentPage++ if @currentPage < @totalPages | |
@redraw() | |
@ | |
prev:-> | |
@currentPage-- if @currentPage > 0 | |
@redraw() | |
@ | |
setSource:(inst,prop)-> | |
@inst = inst | |
@prop = prop | |
@calcTotal() | |
@ | |
redraw:-> | |
startSlice = @currentPage*@perPage | |
endSlice = (@currentPage+1)*(@perPage) | |
data = @getArr().slice startSlice, endSlice | |
@fireCallback 'paginate::redraw', data | |
@ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment