在淘汰赛中实现无限滚动的正确方法是什么? [英] What is the correct way to implement infinite scroll in knockout?

查看:108
本文介绍了在淘汰赛中实现无限滚动的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中有一系列文章,它们很好地呈现为HTML。我想要的是在用户滚动到页面末尾时添加一些新文章。我实现了这一点,但在我看来有一些非常hacky行为:我所做的就是添加jquery事件处理程序 $(window).scroll ,如下所示:

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?

推荐答案

因为没有人回答我的问题,但是Jeroen给了我一个暗示在哪里看,我会尝试用我发现的问题回答我的问题。所以:

Because no one has answered my question, but Jeroen gave me a hint where to look at, I will attempt to answer my question with what I have found. So:

1)你必须使用滚动事件

1) You have to use scroll event

查看

<div id="main" data-bind="foreach: items, event: { scroll: scrolled }">
    <div data-bind="text: name"></div>
</div>

ViewModel

var viewModel = {
    items: ko.observableArray([]),
    scrolled: function(data, event) {
        var elem = event.target;
        if (elem.scrollTop > (elem.scrollHeight - elem.offsetHeight - 200)) {
            getItems(20);
        }
    },
    maxId: 0,
    pendingRequest: ko.observable(false)
};

function getItems(cnt) {
    if (!viewModel.pendingRequest()) {
        var entries = [];
        for (var i = 0; i < cnt; i++) {
            var id = viewModel.maxId++;
            entries.push({
                id: id,
                name: "Name" + id
            });
        }
        viewModel.pendingRequest(true);
        $.ajax({
            type: 'POST',
            url: '/echo/json/',
            data: {json: ko.toJSON(entries), delay: .1},
            success: function(entries) {
                ko.utils.arrayForEach(entries, function(entry) {
                    viewModel.items.push(entry);
                });
                viewModel.pendingRequest(false);
            },
            error: function() {
                viewModel.pendingRequest(false);
            },
            dataType: 'json'
        });
    }
}
ko.applyBindings(viewModel);
getItems(20);

取自这里以及此处的scrollOptions 的类似方法。

这里还有一个很好的麻省理工学院许可证实施

这篇关于在淘汰赛中实现无限滚动的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆