       @$table = $(container).find('table')
       @$context = $(@options.context)
       @instanceId = @constructor._nextId()
-      @invalidateAt = +new Date # timestamp before which we ignore responses
-      @requestAts = [] # list of timestamps for pending requests
       @init()
 
     # Setup and bind to related events
     init: ->
-
-      # Debounce scroll event to improve performance
-      scrollDelay = 250
-      scrollTimeout = null
-      lastCheckAt = null
-      scrollHandler = =>
-        lastCheckAt = +new Date
-        @check()
-
-      # Have we waited enough time since the last check?
-      shouldCheck = -> +new Date > lastCheckAt + scrollDelay
-
-      @$context.scroll ->
-        scrollHandler() if shouldCheck() # Call check once every scrollDelay ms
-        if scrollTimeout
-          clearTimeout(scrollTimeout)
-          scrollTimeout = null
-        scrollTimeout = setTimeout(scrollHandler, scrollDelay)
+      @_listenForScrolling()
 
       # Set a data attribute so we can find again after a turbolink page is loaded
       @$container.attr('data-jquery-infinite-pages-container', @instanceId)
 
-      # Setup callbacks to handle turbolinks page loads
-      $(window.document)
-        .on("page:before-unload", => @_invalidateActiveRequests())
-        .on("page:change", => @_recache())
+      # Setup the callback to handle turbolinks page loads
+      $(window.document).on("page:change", => @_recache())
 
     # Internal helper for logging messages
     _log: (msg) ->

      if typeof @options.loading is 'function'
        @$container.find(@options.navSelector).each(@options.loading)

    _success: ->
      # ignore any requests that started before we last invalidated
      return if @_isInvalidatedRequest(@requestAts.shift())
      @_recache()
      @options.state.loading = false
      @_log "New page loaded!"
      if typeof @options.success is 'function'
        @$container.find(@options.navSelector).each(@options.success)

    _error: ->
      # ignore any requests that started before we last invalidated
      return if @_isInvalidatedRequest(@requestAts.shift())
      @options.state.loading = false
      @_log "Error loading new page :("
      if typeof @options.error is 'function'
        @$container.find(@options.navSelector).each(@options.error)

    # Pause firing of events on scroll
    pause: ->

      @check()

    _recache: ->
      # remove the existing scroll listener
      @$context.off("scroll")
      
      # Recache the element references we use (needed when using turbolinks)
      @$container = $("[data-jquery-infinite-pages-container=#{@instanceId}]")
      @$table = @$container.find('table')
      @$context = $(@options.context)

      @_listenForScrolling()
      
    _listenForScrolling: ->
      # Debounce scroll event to improve performance
      scrollDelay = 250
      scrollTimeout = null
      lastCheckAt = null
      scrollHandler = =>
        lastCheckAt = +new Date
        @check()

      # Have we waited enough time since the last check?
      shouldCheck = -> +new Date > lastCheckAt + scrollDelay

      @$context.scroll ->
        scrollHandler() if shouldCheck() # Call check once every scrollDelay ms
        if scrollTimeout
          clearTimeout(scrollTimeout)
          scrollTimeout = null
        scrollTimeout = setTimeout(scrollHandler, scrollDelay)

    _invalidateActiveRequests: ->
      # Invalidate any active requests (needed when using turbolinks)
