Ghost/ghost/admin/app/mixins/infinite-scroll.js
Kevin Ansfield e01ffa3620 Always call _super when using Ember hooks
no issue
- review use of Ember core hooks and add a call to `this._super` if missing
- fix a few occurrences of using the wrong component lifecycle hooks that could result in multiple/duplicate event handlers being attached

`_super` should always be called when overriding Ember's base hooks so that core functionality or app functionality added through extensions, mixins or addons is not lost. This is important as it guards against issues arising from later refactorings or core changes.

As example of lost functionality, there were a number of routes that extended from `AuthenticatedRoute` but then overrode the `beforeModel` hook without calling `_super` which meant that the route was no longer treated as authenticated.
2015-11-30 12:45:37 +00:00

45 lines
1.1 KiB
JavaScript

import Ember from 'ember';
const {Mixin, run} = Ember;
export default Mixin.create({
isLoading: false,
triggerPoint: 100,
/**
* Determines if we are past a scroll point where we need to fetch the next page
* @param {object} event The scroll event
*/
checkScroll(event) {
let element = event.target;
let triggerPoint = this.get('triggerPoint');
let isLoading = this.get('isLoading');
// If we haven't passed our threshold or we are already fetching content, exit
if (isLoading || (element.scrollTop + element.clientHeight + triggerPoint <= element.scrollHeight)) {
return;
}
this.sendAction('fetch');
},
didInsertElement() {
this._super(...arguments);
let el = this.get('element');
el.onscroll = run.bind(this, this.checkScroll);
if (el.scrollHeight <= el.clientHeight) {
this.sendAction('fetch');
}
},
willDestroyElement() {
this._super(...arguments);
// turn off the scroll handler
this.get('element').onscroll = null;
}
});