2012-07-06 43 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用於滾動窗口。