Skip to content

Instantly share code, notes, and snippets.

@kossnocorp
Created October 5, 2012 05:00
Show Gist options
  • Save kossnocorp/3838187 to your computer and use it in GitHub Desktop.
Save kossnocorp/3838187 to your computer and use it in GitHub Desktop.
loadLocked = false
page = 0
EndlessScroll =
willPaginateEl: (selector = '.pagination') ->
$(selector)
initScroll: (el) ->
el = @willPaginateEl() unless el
return unless el
links = el.find('a')
pages = links.map ->
parseInt \
$(@).attr('href').match(/page=(\d+)$/)[1]
link = links
.filter('[rel="next"]')
.attr('href')
.replace(/\d+$/, '{{page}}')
lastPage = pages.sort()[pages.length - 1]
container = el.parent()
el.remove()
$(window).endlessScroll \
ceaseFireOnEmpty: false,
inflowPixels: 300,
loader: '',
intervalFrequency: 50,
callback: @loadPage.bind(@, link, lastPage, container),
fireOnce: true,
fireDelay: false
loadPage: (link, lastPage, container, fireSequence, pageSequence, direction) ->
return if not(direction is 'next') or
loadLocked
page += 1
stopSpinner = Spin.spin \
container,
'micro',
top: container.height() - 3
loadLocked = true
$.ajax \
url: link.replace('{{page}}', page + 1),
dataType: 'html',
complete: stopSpinner,
success: [
@pageLoaded.bind(@, container, page + 1),
-> loadLocked = false
]
pageLoaded: (container, page, html) ->
response = $(html)
respPagination = response.find('.pagination')
respContainer = respPagination.parent()
respPagination.remove()
container.append \
respContainer.html()
$ ->
EndlessScroll.initScroll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment