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

Total records in infinite mode #257

Open mir3z opened 9 years ago

mir3z commented 9 years ago

Consider following collection:

var Issues = Backbone.PageableCollection.extend({
        url: 'https://api.github.com/search/issues?q=repo:mozilla/BrowserQuest&sort=updated',
        model: Backbone.Model,
        mode: 'infinite',
        state: {
            pageSize: 9
        },
        queryParams: {
            totalPages: null,
            totalRecords: null
        },

        parseState: function (response) {
            return { totalRecords: response.total_count };
        },

        parseRecords: function (response) {
            return response.items;
        }
    });

After fetching this collection, state object will be:

{
    currentPage: 1,
    firstPage: 1,
    lastPage: 4,
    order: -1,
    pageSize: 9,
    sortKey: null,
    totalPages: 4,
    totalRecords: 35
}

This is incorrect since there are 26 issues returned by following request: https://api.github.com/search/issues?q=repo:mozilla/BrowserQuest&sort=updated&per_page=9&page=1

wyuenho commented 8 years ago

Not sure what's going on here. That's a query results different results as time goes by. Please ping me again when you have a confirmed case.

ogonkov commented 6 years ago

@wyuenho I seems have similar issue. My server response with total records, but then it get incremented on each model add

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

Demo of the problem https://jsfiddle.net/ogonkov/vugfj02n/

While server responded with 5 records in collection and results_count (totalRecords) of 10, final state is 3 pages, and 15 records.

ogonkov commented 6 years ago

This behaviour seems affects all versions since >=2.0.0 (haven't tested on 1.x)

wyuenho commented 6 years ago

There are so many things that could go wrong in your fiddle I can't figure out what's wrong. If you can simplify the fiddle to its bare essentials then I might be able to help.

ogonkov commented 6 years ago

@wyuenho i have simplified example: removed mock stringifying, and log all state changes to console instead of DOM. Please have a look https://jsfiddle.net/ogonkov/vugfj02n/21/

hopkinschris commented 5 years ago

@ogonkov Hey, were you ever able to resolve this issue?

ogonkov commented 5 years ago

@hopkinschris not yet. I feel there should be some refactoring, to simplify thing that should happen during collection lifecycles. We need to know that totalRecords was changed during last sync, and take it into account, when we handle models add. We need to react to state change, in more smart way.

I have a kind of road map for this in my head: