14

我正在玩backbone.js,我想知道是否有比我一直在查看的模块更简洁的解决方案来为模型/集合创建“无限滚动”情况(有几个用于jquery,可能更多其他图书馆)。

一些搜索几乎没有出现,所以我想在尝试使用主干构建我自己的解决方案之前先问一下,我是否应该在主干集合之外构建它,或者是否有人尝试过类似的东西。

4

6 回答 6

10

这篇文最近登陆了我的推特流。看起来是一个非常好的解决方案,并且在主页上运行良好。从博客来看,这个解决方案的好处是:

  1. 保留后退按钮。
  2. 不使用 hashbang,不管 twitter 工程师对它有多满意。
  3. 如果 (1) 不可能,则放弃无限滚动。
  4. 逐步增强:当 (3) 发生时,用户跟随一个好的 ol' 超链接以获得更多内容。
于 2011-05-23T20:17:34.747 回答
4

我自己研究时发现了另外两个资源:

  1. https://github.com/joneath/infiniScroll.js
  2. http://addyosmani.com/blog/backbone-paginator-new-pagination-components-for-backbone-js/
于 2012-04-10T01:42:37.983 回答
1

如果您还没有尝试过,SlickGrid 可能是一个选择:https ://github.com/mleibman/SlickGrid/wiki/Examples

于 2011-05-23T21:50:48.310 回答
0

使用 Backbone 和 jQuery 航点的另一个解决方案:https ://gist.github.com/davidmontoyago/6336612

于 2013-08-30T14:37:09.887 回答
0

以下扩展应该可以工作: https ://github.com/joneath/infiniScroll.js

这些对于部分加载数据也可能有用: http ://backplug.io/plugin/Backbone.actAs.Paginatable、https : //github.com/wyuenho/backbone-pageable、https : //github.com/backbone-分页器/backbone.paginator

我更喜欢在这里搜索 Backbone 扩展:http: //backplug.io

于 2013-08-30T16:44:55.317 回答
-1

另一个重复的答案:这是我对 Backbone.Collection 的扩展:

_.extend Backbone.Collection.prototype,
  options:
    infinitescroll:
      success: $.noop
      error: $.noop
      bufferPx: 40
      scrollPx: 150
      page:
        current: 0
        per: null
      state:
          isDuringAjax: false
          isDone: false
          isInvalid: false
      loading:
        wrapper: 'backbone-infinitescroll-wrapper'
        loadingId: 'backbone-infinitescroll-loading'
        loadingImg: 'loading.gif'
        loadingMsg: '<em>Loading ...</em>'
        finishedMsg: '<em>No more</em>'
        msg: null
        speed: 'fast'

  infinitescroll: (options={})->
    # NOTE: coffeescript cannot deal with nested scope!
    that = @

    _.extend(@options.infinitescroll, options.infinitescroll) if options.infinitescroll

    infinitescroll_options = @options.infinitescroll

    # where we want to place the load message in?
    infinitescroll_options.loading.wrapper = $(infinitescroll_options.loading.wrapper)
    if !infinitescroll_options.loading.msg and infinitescroll_options.loading.wrapper.size() > 0
      infinitescroll_options.loading.msg = $('<div/>', {
        id: infinitescroll_options.loading.loadingId
      }).html('<img alt="'+$(infinitescroll_options.loading.loadingMsg).text()+'" src="' + infinitescroll_options.loading.loadingImg + '" /><div>' + infinitescroll_options.loading.loadingMsg + '</div>')
      infinitescroll_options.loading.msg.appendTo(infinitescroll_options.loading.wrapper).hide()
    else
      infinitescroll_options.loading.msg = null

    fetch_options = _.omit(options, 'infinitescroll')
    infinitescroll_fetch = ()=>
      # mark the XHR request
      infinitescroll_options.state.isDuringAjax = true

      # increase page count
      infinitescroll_options.page.current++

      payload = {
        page: infinitescroll_options.page.current
      }
      payload['limit'] = infinitescroll_options.page.per if infinitescroll_options.page.per isnt null

      _.extend(fetch_options, {
        remove: false
        data: payload
      })

      if infinitescroll_options.loading.msg
        # preload loading.loadingImg
        (new Image()).src = infinitescroll_options.loading.loadingImg if infinitescroll_options.loading.loadingImg

        infinitescroll_options.loading.msg.fadeIn infinitescroll_options.loading.speed, ()->
          that.fetch(fetch_options)
          .success (data, state, jqXHR)=>
            infinitescroll_options.state.isDuringAjax = false
            infinitescroll_options.state.isDone = true if _.size(data) is 0

            infinitescroll_options.loading.msg.fadeOut infinitescroll_options.loading.speed, ()->
              infinitescroll_options.success.call(data, state, jqXHR) if _.isFunction(infinitescroll_options.success)
              return
            return
          .error (data, state, jqXHR)=>
            infinitescroll_options.state.isDuringAjax = false
            infinitescroll_options.state.isInvalid = true

            infinitescroll_options.loading.msg.fadeOut infinitescroll_options.loading.speed, ()->
              infinitescroll_options.error.call(data, state, jqXHR) if _.isFunction(infinitescroll_options.error)
              return
            return
          return

      else
        that.fetch(fetch_options)
        .success (data, state, jqXHR)=>
          infinitescroll_options.state.isDuringAjax = false
          infinitescroll_options.state.isDone = true if _.size(data) is 0

          infinitescroll_options.success.call(data, state, jqXHR) if _.isFunction(infinitescroll_options.success)
          return

        .error (data, state, jqXHR)=>
          infinitescroll_options.state.isDuringAjax = false
          infinitescroll_options.state.isInvalid = true

          infinitescroll_options.error.call(data, state, jqXHR) if _.isFunction(infinitescroll_options.error)
          return
      return

    $(document).scroll ()->
      $doc = $(document)
      isNearBottom = ()->
        bottomPx = 0 + $doc.height() - $doc.scrollTop() - $(window).height()

        # if distance remaining in the scroll (including buffer) is less than expected?
        (bottomPx - infinitescroll_options.bufferPx) < infinitescroll_options.scrollPx

      return if infinitescroll_options.state.isDuringAjax || infinitescroll_options.state.isDone || infinitescroll_options.state.isInvalid || !isNearBottom()

      infinitescroll_fetch()
      return


    infinitescroll_fetch()
    return

https://gist.github.com/mcspring/7655861

于 2013-11-26T10:08:00.393 回答