backbone-paginator/backbone-pageable

setting values in backbone url function in infinite mode.

jeacott opened this issue · 3 comments

Hi,
I'm experiencing 2 issues with infinite mode that arent a problem in server mode.

I have a PageableCollection as below,
first, I cant seem to pass options to it (using new PageableItemCollection (
[],{id:"someid"}
);
the options param is always null inside the initialize function, but it works fine if I switch the mode to 'server'

and second, when its in server mode, the fetch/getNext functions properly construct the url
including the appended 'id',
but when in infinite mode, the value of id is always null, so never appended. (even if I hard code it inside the initialize)
how should I be dealing with this? I tried setting the id on the fullCollection too but it didnt help.

any help appreciated.
javascript is not my forte so its likely I'm doing something wrong :)

PageableItemCollection = Backbone.PageableCollection.extend({
        url:function(){
            return "api/timeline"+((this.id==null)?"":"/"+this.id);
        },
        initialize: function(models,options) {
            this.id = options.id;
            Backbone.PageableCollection.prototype.initialize.apply(this, options);
},
 mode:'infinite',
        state: {
            firstPage: 0,
            pageSize: 2,
            sortKey: "modifiedDate",
            totalRecords: null
        },

ok, so my first issue was a simple mistake and should be ignored, but I cannot get the id to turn up in the url function on the initial fetch.
if I add the following parseLinks handler then I get what I want for subsequent requests, the id is properly appended to the url path.
but the first request is always wrong. thoughts?

parseLinks: function (resp, xhr) {
            return {
                "next": this.url()
                };
          }

The URL is assumed to be an unbound method and is not given a context when called, Backbone.Collection behaves exactly the same.

What you want to do is to bind your url() to the pageable collection instance.

var col = new MyPageableCollection();
col.url = _.bind(function () {
  return "api/timeline"+((this.id==null)?"":"/"+this.id);
}, col);

Hi, thank you for responding.
If the behaviour is standard then why does it change depending on the mode? I don't have any trouble with an ordinary backbone.collection, and the backbone.pageable collection in server mode works as expected. its only infinite mode where things change.
I don't understand why the behaviour should be different, but it seems its by design.
thank you for posting a solution.