Gitter migration: Point people to app.gitter.im (rollout pt. 1)
[gitter.git] / public / js / views / behaviors / infinite-scroll.js
bloba08ec9d36c0972c9cad4756549633ae66ce48fd9
1 'use strict';
2 var Marionette = require('backbone.marionette');
3 var behaviourLookup = require('./lookup');
4 var NeverEndingStory = require('../../utils/never-ending-story');
6 module.exports = (function() {
7   var Behavior = Marionette.Behavior.extend({
8     defaults: {
9       reverseScrolling: false,
10       scrollElementSelector: null,
11       contentWrapperSelector: null
12     },
13     initialize: function() {
14       var scrollElement =
15         this.options.scrollElement ||
16         document.querySelector(this.options.scrollElementSelector) ||
17         this.view.el;
19       var contentWrapper =
20         this.options.contentWrapper || document.querySelector(this.options.contentWrapperSelector);
22       var reverseScrolling = this.options.reverseScrolling;
24       var scroll = new NeverEndingStory(scrollElement, {
25         reverse: reverseScrolling,
26         contentWrapper: contentWrapper
27       });
29       this.listenTo(scroll, 'approaching.top', function() {
30         this.view.collection.fetchMoreBefore({});
31       });
33       this.listenTo(scroll, 'approaching.bottom', function() {
34         this.view.collection.fetchMoreAfter({});
35       });
37       this.scroll = scroll;
38       this.view.scroll = scroll;
39     },
41     onDestroy: function() {
42       this.scroll.disable();
43     }
44   });
46   behaviourLookup.register('InfiniteScroll', Behavior);
47   return Behavior;
48 })();