View not firing中的滚动事件(backbone.js)

时间:2012-07-06 13:34:39

标签: javascript jquery backbone.js underscore.js

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

查看

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代码中插入它?以下是我目前放置它的地方。

ROUTER

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();

1 个答案:

答案 0 :(得分:1)

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

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

相关问题