使用Paginator.clientPager从服务器加载额外的模式



我试图在Paginator.clientPager初始获取后从服务器加载其他模态

这是我的集合,基本上是从github上的示例代码复制粘贴的。

return new (Backbone.Paginator.clientPager.extend({
    model: model,
    paginator_core: {
        type: 'GET',
        dataType: 'json',
        url: '/odata/LibraryFile'
    },
    paginator_ui: {
        // the lowest page index your API allows to be accessed
        firstPage: 1,
        // which page should the paginator start from
        // (also, the actual page the paginator is on)
        currentPage: 1,
        // how many items per page should be shown
        perPage: 2,
        // a default number of total pages to query in case the API or
        // service you are using does not support providing the total
        // number of pages for us.
        // 10 as a default in case your service doesn't return the total
        totalPages: 5
    },
    server_api: {
        // number of items to return per request/page
        '$skip': function () { return this.perPage * (this.currentPage - 1) },
        '$top': function () { return this.perPage },
    },
    parse: function (response) {
        console.log(response);
        return response.value;
    }
}))();

我像这样调用初始取回

myCollection.fetch({
    success: function(){
        myCollection.pager();
    },
    silent:true
});

然后,在用户使用clientPager浏览了本地页面之后,他可能希望加载更多的页面,而不删除第一个页面。

我试着这样做,但由于某种原因,在我调用pager();后,2条新记录被删除。

myCollection.currentPage = 2;
myCollection.fetch({
    success: function(){ 
        console.log(myCollection.length) // 4 models, with correct data
        myCollection.pager();
        console.log(myCollection.length) // the 2 new records are removed
    },
    silent:true,
    remove: false // don't remove old records
});

我做错了什么,我怎么能加载它2更多的Paginator.clientPager页?

我不想使用requestPager,因为我不能在内存预缓存,至少,我认为。

根据我的经验,这是由Backbone.Paginator.clientPager的pager()方法引起的。你可以看看下面的代码:Backbone.Paginator.clientPager

第292到294行显示Backbone.Paginator.clientPager. clientpager .

如果未定义,origModels

仅分配给当前模型(您在上面的插图中正确测试了其长度的模型)。问题是,当用户可能想要加载更多页面而不删除第一个时,origModels属性已经作为初始获取的结果设置好了。

这意味着你必须显式地使origModels未定义之前,pager()将按照你想要的方式行事。注意在源代码的第296行上发生的事情(models被分配给了origModels的一个副本)。所以你的两张新唱片被删除了。下面的代码应该可以正常工作:

myCollection.currentPage = 2;
myCollection.fetch({
    success: function(){ 
        delete myCollection.origModels; // to ensure that origModels is overridden in pager() call below
        myCollection.pager();
    },
    silent:true,
    remove: false // don't remove old records
});

最新更新