Javascript 使用Paginator.clientPager从服务器加载其他模态
在使用Javascript 使用Paginator.clientPager从服务器加载其他模态,javascript,backbone.js,backbone-paginator,Javascript,Backbone.js,Backbone Paginator,在使用Paginator.clientPager 这是我的收藏,基本上是从github上的示例代码粘贴而来 return new (Backbone.Paginator.clientPager.extend({ model: model, paginator_core: { type: 'GET', dataType: 'json', url: '/odata/LibraryFile' }, paginator_u
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
});
我做错了什么,如何使用Paginator.clientPager
再加载2页
我不想使用requestPager,因为这样我就不能在内存中进行预缓存,至少我认为是这样。根据我的经验,这是由Backbone.Paginator.clientPager的pager()方法造成的。您可以在此处查看代码:
第292行到第294行显示主干.Paginator.clientPager.origModels仅分配给当前模型(您在上图中正确测试了其长度的模型),如果未定义。问题是,当用户可能希望加载更多页面而不删除第一个页面时,初始获取的结果是origModels属性已经设置好了
这意味着您必须显式地重新定义origModels,然后pager()才能按您的意愿运行。请注意源代码第296行后面发生的事情(模型被分配给原始模型的副本)。这就是你的两张新唱片被删除的原因。以下代码应按预期工作:
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
});
根据我的经验,这是由Backbone.Paginator.clientPager的pager()方法引起的。您可以在此处查看代码:
第292行到第294行显示主干.Paginator.clientPager.origModels仅分配给当前模型(您在上图中正确测试了其长度的模型),如果未定义。问题是,当用户可能希望加载更多页面而不删除第一个页面时,初始获取的结果是origModels属性已经设置好了
这意味着您必须显式地重新定义origModels,然后pager()才能按您的意愿运行。请注意源代码第296行后面发生的事情(模型被分配给原始模型的副本)。这就是你的两张新唱片被删除的原因。以下代码应按预期工作:
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
});
这里太安静了:)这里太安静了:)