backbone-paginator/backbone-pageable

Infinite mode not parsing server state

robj opened this issue · 11 comments

robj commented

ie. no mode set:

currentPage: 1
firstPage: 1
lastPage: 3
order: -1
pageSize: 10
sortKey: null
totalPages: 3
totalRecords: null


setting mode:infinite with no other changes

currentPage: 1
firstPage: 1
lastPage: 1
order: -1
pageSize: 10
sortKey: null
totalPages: 1
totalRecords: 10

I use an overridden parseState

parseState: function (resp, queryParams, state, options) {

return {

    totalPages: parseInt(options.xhr.getResponseHeader("total_pages")),
    lastPage:  parseInt(options.xhr.getResponseHeader("total_pages")),

    pageSize: parseInt(options.xhr.getResponseHeader("per_page")),
    currentPage: parseInt(options.xhr.getResponseHeader("current_page")),

What's a "total_pages" header? Those aren't even valid HTTP header formats. Could it be the reason why your browser's XHR can't get it? Fix that on the server and try again. Or just pass the state info into the result JSON and adjust your parseState for that.

robj commented

Using custom headers is not an issue, http://tools.ietf.org/search/rfc6648. Parsing these values works fine if infinite mode is not enabled.

Can you make a jsFiddle or Codepen? And what are the initial state and the expected state?

And what are the values in contained in the headers?

robj commented

I don't think I can really in this case, as its interacting with a development server.

In case 1, no mode set. The values are parsed successfully ( totalPages=3, lastPage=3 , pageSize=10 )

in case 2, mode=infinite, the values are not parsed properly.

the headers are

total_pages = 3 (totalPages, lastPage)
per_page = 10 ( pageSize)
current_page =1 (currentPage)

It is parsing server state, it's just the state was overridden again when the success handler inside fetch adds to the full collection after the state has already been updated. I think it's a duplicate of #136 . If you believe it's not and can provide the exact steps to reproduce it, reopen this issue.

Ok this is a different but related issue.

Mmm actually I might just revert this.

You shouldn't have to supply your own state during infinite paging. All you need are just the link for the next page and records for the page you just fetched. The state will be recalculated completely on the client side. The docs makes no mention of having to override parseState during infinite mode either. I should probably make explicit that overriding parseState under infinite mode will mess up your state tho.

I've updated the README to clarify this.

7f799b2

robj commented

In this case, I overrode parseState and used headers as it suggested as an example in the API docs for this project.

linked from the README

'You can consult the API documentation for a detailed explaination of these fields.'

http://backbone-paginator.github.io/backbone-pageable/api/#!/api/Backbone.PageableCollection-property-state

and API docs state:

If you want to use header fields use:

parseState: function (resp, queryParams, state, options) {
return {totalRecords: options.xhr.getResponseHeader("X-total")};
}
This method MUST return a new state object instead of directly modifying the state object. The behavior of directly modifying state is undefined.

The README and the parseState API doc didn't mention this method is applicable during infinite mode. I just assumed that everyone would have come to the conclusion that it isn't applicable. I just clarified that in the README.