既然你have've没有发布的代码片段我解释,你可以做各种可能的方法之一。 您可以更改您的服务器端代码,以便能够处理延迟加载的方式返回数据。
然后,你需要有一个间隔函数,它继续调用一个函数,该函数在你检查一个标志,如果你最后的ajax调用返回数据,然后调用下一个ajax。
在您的响应数据中,如果有更多数据,总项目等,那么可以跟踪数据。然后在您的Ajax成功回调中更新observables变量,您可以使用它们发送请求数据(如NextPage等),如果没有更多的数据,你取消了setInterval函数。
下面仅仅是如何实现这种方法(未测试)
JS的例子:
var YourViewModel = function() {
var self = this ;
var loadMoreTimer;
self.IsMoreItems = ko.observable(false);
self.LoadingMoreInProcess = ko.observable(false);
self.CurrentPage = ko.observable(0);
self.Items = ko.observableArray([]);
self.AutomaticLoadItem = function() {
loadMoreTimer = setInterval(callLoadMore, 500);
}
function callLoadMore() {
if (self.IsMoreItems()) {
if (!self.LoadingMoreInProcess()) {
self.LoadMore();
}
}
}
self.LoadMore = function() {
self.CurrentPage(self.CurrentPage() + 1);
self.LoadingMoreInProcess(true);
data = {requestPage : self.CurrentPage(), numberOfItem : 20, ...}
//your ajax call here
$.ajax({
url:...,
type: ....,
data: data,
success: function(response){
// here you check if there is more data left
if(response.isMoreData){
// set it to false so the next ajax call can be executed.
self.LoadingMoreInProcess(false);
// map through your data and push each item into the observableArray
$.map(response.Items, function (item) {
self.Items.push(new ItemDetailViewModel(item));
});
}else{
// if there is no more data stop the interval function.
clearInterval(loadMoreTimer);
}
}
})
}
//Call to start lazy loading
self.AutomaticLoadItem();
}
var ItemDetailViewModel = function (data){
var self = this ;
self.Id = data.Id ;
self.Name = data.Name;
// .....
}
你可以放在一起能重现问题的jsfiddle或片段? – JohnnyHK
使用这里讨论的技术大大提高了我在大数据集下的应用程序性能 - http://www.knockmeout.net/2012/04/knockoutjs-performance-gotcha.html – dmoo
检出https://github.com/brianmhunt/knockout -fast-的foreach –