2015-02-12 21:22:32 -07:00
|
|
|
import Ember from 'ember';
|
2014-07-20 12:42:03 -04:00
|
|
|
var PaginationViewInfiniteScrollMixin = Ember.Mixin.create({
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if we are past a scroll point where we need to fetch the next page
|
2014-10-24 21:09:50 +00:00
|
|
|
* @param {object} event The scroll event
|
2014-07-20 12:42:03 -04:00
|
|
|
*/
|
|
|
|
checkScroll: function (event) {
|
|
|
|
var element = event.target,
|
|
|
|
triggerPoint = 100,
|
|
|
|
controller = this.get('controller'),
|
|
|
|
isLoading = controller.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.send('loadNextPage');
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind to the scroll event once the element is in the DOM
|
|
|
|
*/
|
2014-09-09 19:22:11 -06:00
|
|
|
attachCheckScroll: function () {
|
2015-01-07 00:16:16 +01:00
|
|
|
var el = this.$(),
|
|
|
|
controller = this.get('controller');
|
2014-07-20 12:42:03 -04:00
|
|
|
|
|
|
|
el.on('scroll', Ember.run.bind(this, this.checkScroll));
|
2015-01-07 00:16:16 +01:00
|
|
|
|
|
|
|
if (this.element.scrollHeight <= this.element.clientHeight) {
|
|
|
|
controller.send('loadNextPage');
|
|
|
|
}
|
2015-06-02 20:56:42 -06:00
|
|
|
},
|
2014-07-20 12:42:03 -04:00
|
|
|
|
2015-06-02 20:56:42 -06:00
|
|
|
didInsertElement: function () {
|
|
|
|
this.attachCheckScroll();
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement: function () {
|
|
|
|
// unbind from the scroll event when the element is no longer in the DOM
|
|
|
|
this.$().off('scroll');
|
|
|
|
}
|
2014-07-20 12:42:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
export default PaginationViewInfiniteScrollMixin;
|