0

我的backbone.js 视图中有一个scroll事件。但是,当我滚动屏幕时,scroll似乎没有触发事件处理程序。$(windows).scroll()虽然工作正常。这是否意味着该scroll事件不能用于 Views?

看法

PhotoListView = Backbone.View.extend({
    el: '#photo_list',

    events: {
        'scroll': function() {
            console.log('scrolling!');
        }
    },

    initialize: function() {
        this.collection.bind('reset', this.render, this);
    },

    render: function() {
        // ...
        }, this);
        return this;
    }
});

另外,如果我想用它$(windows).scroll()来处理滚动事件,我应该将它插入到backbone.js代码的哪个部分?下面是我目前放置的位置。

路由器

var AppRouter = Backbone.Router.extend({
    routes: {
        '': 'explore'
    },

    explore: function() {
        this.photoList = new PhotoCollection();
        var self = this;
        this.photoList.fetch({
            success: function() {
                self.photoListView = new PhotoListView({ collection: self.photoList });
                self.photoListView.render();

                // Check for Scrolling
                $(window).scroll(function() {
                    self.checkScroll();
                });
            }
        });
    },

    checkScroll: function() {
        var contentOffset = $('#photo_list').offset().top;
        var contentHeight = $('#photo_list').height();
        var pageHeight = $(window).height();
        var scrollTop = $(window).scrollTop();
        var triggerPoint = 100;

        if(contentOffset + contentHeight - scrollTop - pageHeight < triggerPoint) {

            this.photoListView.collection.requestNextPage()
                .done(function(data, textStatus, jqXHR) {
            });

        }
    }

});

var app = new AppRouter();
Backbone.history.start();
4

1 回答 1

1

滚动处理程序将在您滚动元素时触发,"#photo_list"因为它已绑定在该元素上。

$(window).scroll用于滚动窗口时。

于 2012-07-06T13:41:00.587 回答