pager.js.coffee 950 Bytes
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
@Pager =
2
  init: (@limit = 0, preload, @disable = false) ->
3 4
    @loading = $('.loading').first()

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5 6 7 8
    if preload
      @offset = 0
      @getOld()
    else
9
      @offset = @limit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10 11 12
    @initLoadMore()

  getOld: ->
13
    @loading.show()
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14 15
    $.ajax
      type: "GET"
16
      url: $(".content_list").data('href') || location.href
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
      data: "limit=" + @limit + "&offset=" + @offset
18 19
      complete: =>
        @loading.hide()
20 21 22
      success: (data) ->
        Pager.append(data.count, data.html)
      dataType: "json"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
23 24 25 26 27 28 29 30 31

  append: (count, html) ->
    $(".content_list").append html
    if count > 0
      @offset += count
    else
      @disable = true

  initLoadMore: ->
32
    $(document).unbind('scroll')
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
33 34 35 36 37 38 39
    $(document).endlessScroll
      bottomPixels: 400
      fireDelay: 1000
      fireOnce: true
      ceaseFire: ->
        Pager.disable

40 41 42 43
      callback: (i) =>
        unless @loading.is(':visible')
          @loading.show()
          Pager.getOld()