Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if in view on resize events #45

Merged
merged 1 commit into from
Jun 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions addon/components/infinity-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default Ember.Component.extend({
classNames: ["infinity-loader"],
classNameBindings: ["infinityModel.reachedInfinity"],
guid: null,
scrollDebounce: 10,
eventDebounce: 10,
loadMoreAction: 'infinityLoad',
loadingText: 'Loading Infinite Model...',
loadedText: 'Infinite Model Entirely Loaded.',
Expand All @@ -18,23 +18,25 @@ export default Ember.Component.extend({
this._super(...arguments);
this._setupScrollable();
this.set('guid', Ember.guidFor(this));
this._bindScroll();
this._bindEvent('scroll');
this._bindEvent('resize');
this._checkIfInView();
},

willDestroyElement() {
this._super(...arguments);
this._unbindScroll();
this._unbindEvent('scroll');
this._unbindEvent('resize');
},

_bindScroll() {
this.get("scrollable").on(`scroll.${this.get('guid')}`, () => {
Ember.run.debounce(this, this._checkIfInView, this.get('scrollDebounce'));
_bindEvent(eventName) {
this.get('scrollable').on(`${eventName}.${this.get('guid')}`, () => {
Ember.run.debounce(this, this._checkIfInView, this.get('eventDebounce'));
});
},

_unbindScroll() {
this.get("scrollable").off(`scroll.${this.get('guid')}`);
_unbindEvent(eventName) {
this.get('scrollable').off(`${eventName}.${this.get('guid')}`);
},

_checkIfInView() {
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/components/infinity-loader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,42 @@ test('it throws error when multiple scrollable elements are found', function(ass
}, Error, "Should raise error");
});

test('it checks if in view on the scroll event', function(assert) {
assert.expect(1);

var component = this.subject();

var isAfterRender = false;
component.set('_checkIfInView', function() {
if (isAfterRender) {
assert.ok(true);
}
});

this.render();

isAfterRender = true;
$(window).trigger('scroll');
});

test('it checks if in view on the resize event', function(assert) {
assert.expect(1);

var component = this.subject();

var isAfterRender = false;
component.set('_checkIfInView', function() {
if (isAfterRender) {
assert.ok(true);
}
});

this.render();

isAfterRender = true;
$(window).trigger('resize');
});

test('it checks if in view after model is pushed', function(assert) {
assert.expect(4);

Expand Down