backbone-paginator / backbone.paginator

A pageable, drop-in replacement for Backbone.Collection called Backbone.PageableCollection.
http://backbone-paginator.github.io/backbone.paginator/
MIT License
1.41k stars 254 forks source link

Feature request: Infinite mode with total records #385

Open tilwinjoy opened 6 years ago

tilwinjoy commented 6 years ago

I have an API that returns the number of records, but not the "next", "previous" headers/response data. I want to fetch info from this API using infinite scroll.

Currently if I try to use "server" mode with total records every request will clear previous models. If I try to use "infinite" mode I need to generate the links myself.

It'd be cool if the plugin requests next page as in "server" mode when "total_records" is available instead of looking for links in "infinite" mode.

ogonkov commented 6 years ago

I think lib will expect to have actual models aswell, so beside having links for all pages, you probably need to generate (and then update) some stub models. I believe that lib just slice models from fullCollection, without actually fetching it.

https://github.com/backbone-paginator/backbone.paginator/blob/1766e8f67171a7ee292de3874dd006898929489c/lib/backbone.paginator.js#L912-L916

Links persistent checked few lines above

https://github.com/backbone-paginator/backbone.paginator/blob/1766e8f67171a7ee292de3874dd006898929489c/lib/backbone.paginator.js#L903

I face the problem that i want reuse already fetched models, and having a paging, that means that i need to load pages from first to last sequentially, to prevent fullCollection break.

As a temporarily "fix" i have override getPage method to switch to server mode for initial fetch, but it will break, if next page would be >1 from the current (no link for it in state)

import PageableCollection from 'backbone.paginator';

export class Foo extends PageableCollection {
  getPage(index, options) {
    const useServerMode = (
      index > this.state.firstPage &&
      this.length === 0 &&
      this.mode === 'infinite'
    );

    if (useServerMode) {
      const fetch = false; // Prevent unnecessary fetch while switching modes

      this.switchMode('server', {
        fetch
      });

      this.once('sync', function() {
        this.switchMode('infinite', {
          fetch
        });
      }, this);
    }

    return super.getPage(index, options)
  }
}
wyuenho commented 6 years ago

@ogonkov I've invited you as a collaborator of this repo. Since I don't use this library much anymore, it'd be nicer for someone who knows what's going on to help maintain it.

ogonkov commented 6 years ago

@wyuenho thank you