I have an array of articles in my Model and they are rendered nicely as HTML. What I want is to add some new articles when the user scrolls to the end of the page. I achieved this, but in my opinion with some really hacky behavior: all I have done is added jquery event handler $(window).scroll
, like this:
function ArticlesViewModel() {
var self = this;
this.listOfReports = ko.observableArray([]);
this.loadReports = function() {
$.get('/router.php', {type: 'getReports'}, function(data){
self.listOfReports(self.listOfReports().concat(data));
}, 'json');
};
this.loadReports();
$(window).scroll(function() {
if($(window).scrollTop() == $(document).height() - $(window).height()) {
self.loadReports();
}
})
};
In my simple prototype scenario it works nicely, but I think that this scroll will be called even if I will hide my model.
So is there a more appropriate way to do the same behavior?