Skip to content
This repository has been archived by the owner on Jul 4, 2021. It is now read-only.

Feature request: Infinite mode with total records #385

Open
tilwinjoy opened this issue Jan 5, 2018 · 3 comments
Open

Feature request: Infinite mode with total records #385

tilwinjoy opened this issue Jan 5, 2018 · 3 comments

Comments

@tilwinjoy
Copy link

tilwinjoy commented Jan 5, 2018

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
Copy link
Collaborator

ogonkov commented Aug 10, 2018

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.

if ((mode == "client" || (mode == "infinite" && !_isEmpty(pageModels))) &&
!options.fetch) {
this.reset(pageModels, _omit(options, "fetch"));
return this;
}

Links persistent checked few lines above

this.state = this._checkState(_extend({}, state, {currentPage: pageNum}));

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
Copy link
Member

wyuenho commented Aug 10, 2018

@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
Copy link
Collaborator

ogonkov commented Aug 11, 2018

@wyuenho thank you

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants