I have tableone that uses infinite scrolling to load more results and add them when the user reaches the bottom of the page.
I currently have the following code:
var currentPage = 0;
var tableContent = Rx.Observable.empty();
function getHTTPDataPageObservable(pageNumber) {
return Rx.Observable.fromPromise($http(...));
}
function init() {
reset();
}
function reset() {
currentPage = 0;
tableContent = Rx.Observable.empty();
appendNextPage();
}
function appendNextPage() {
if(currentPage == 0) {
tableContent = getHTTPDataPageObservable(++currentPage)
.map(function(page) { return page.content; });
} else {
tableContent = tableContent.combineLatest(
getHTTPDataPageObservable(++currentPage)
.map(function(page) { return page.content; }),
function(o1, o2) {
return o1.concat(o2);
}
)
}
}
There is one main problem :
Called every time appendNextPage, I get a completely new one Observable, which then calls all previous HTTP calls over and over again.
A minor issue is that this code is ugly, and it looks too much like such a simple use case.
Questions:
How to solve this problem in a beautiful way?
Observables -, ?