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