2012-07-06 76 views
0

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

VIEW

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的代码,我应该插入哪一部分?以下是我目前所在的位置。当您滚动元素"#photo_list",因为它是该元素的结合

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

当你滚动元素''#photo_list''时,滚动处理程序将触发,因为它绑定在那个元素上......它甚至可以滚动吗? – Esailija 2012-07-06 13:38:06

+0

哦'#photo_list'元素没有滚动条。我想我需要在路由器中使用'$(window).scroll()',就像上面的代码一样? – Nyxynyx 2012-07-06 13:39:33

+0

是的,'$(window).scroll'用于当你滚动窗口时...'$(element).scroll'用于当你滚动那个元素时 – Esailija 2012-07-06 13:40:07

回答

1

滚动处理程序将闪光。

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