Skip to content

Commit

Permalink
Merge pull request #248 from DockYard/sn/bug-once
Browse files Browse the repository at this point in the history
[BUG]: Unable to use multiple {{in-viewport}} modifiers
  • Loading branch information
snewcomer authored Aug 2, 2020
2 parents 7821979 + 2885c2b commit 2d8f6f7
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 14 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class MyClass extends Component {
@service inViewport

@action
didInsertNode() {
setupInViewport() {
const loader = document.getElementById('loader');
const viewportTolerance = { bottom: 200 };
const { onEnter, _onExit } = this.inViewport.watchElement(loader, { viewportTolerance });
Expand Down Expand Up @@ -312,7 +312,7 @@ import InViewportMixin from 'ember-in-viewport';

export default class Infinity extends Component.extend(InViewportMixin) {
@action
didInsertNode(element) {
setupInViewport(element) {
set(this, 'viewportSpy', true);
set(this, 'viewportTolerance', {
bottom: 300
Expand All @@ -329,7 +329,7 @@ export default class Infinity extends Component.extend(InViewportMixin) {
```

```hbs
<div {{did-insert this.didInsertNode}}>
<div {{did-insert this.setupInViewport}}>
{{yield}}
</div>
```
Expand All @@ -347,7 +347,7 @@ export default class MyClass extends Component {
@service inViewport

@action
didInsertNode() {
setupInViewport() {
const loader = document.getElementById('loader');
const viewportTolerance = { bottom: 200 };
const { onEnter, _onExit } = this.inViewport.watchElement(loader, { viewportTolerance });
Expand Down Expand Up @@ -380,7 +380,7 @@ export default class MyClass extends Component {
@service inViewport

@action
didInsertNode(element) {
setupInViewport(element) {
const viewportTolerance = { bottom: 200 };
const { onEnter, onExit } = this.inViewport.watchElement(element, { viewportTolerance });
onEnter(this.didEnterViewport.bind(instance));
Expand All @@ -401,7 +401,7 @@ export default class MyClass extends Component {
```

```hbs
<div {{did-insert this.didInsertNode}}>
<div {{did-insert this.setupInViewport}}>
{{yield}}
</div>
```
Expand Down
8 changes: 4 additions & 4 deletions addon/services/in-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { set, setProperties } from '@ember/object';
import { assign } from '@ember/polyfills';
import { getOwner } from '@ember/application';
import { warn } from '@ember/debug';
import { scheduleOnce } from '@ember/runloop';
import { schedule } from '@ember/runloop';
import isInViewport from 'ember-in-viewport/utils/is-in-viewport';
import canUseRAF from 'ember-in-viewport/utils/can-use-raf';
import canUseIntersectionObserver from 'ember-in-viewport/utils/can-use-intersection-observer';
Expand Down Expand Up @@ -63,12 +63,12 @@ export default class InViewport extends Service {
}
const observerOptions = this.buildObserverOptions(configOptions);

scheduleOnce('afterRender', this, this.setupIntersectionObserver, element, observerOptions, enterCallback, exitCallback);
schedule('afterRender', this, this.setupIntersectionObserver, element, observerOptions, enterCallback, exitCallback);
} else {
if (!this.rafAdmin) {
this.startRAF();
}
scheduleOnce('afterRender', this, this._startRaf, element, configOptions, enterCallback, exitCallback);
schedule('afterRender', this, this._startRaf, element, configOptions, enterCallback, exitCallback);
}

return {
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class InViewport extends Service {

/**
* In order to track elements and the state that comes with them, we need to keep track
* of them in order to get at them at a later time
* of elements in order to get at them at a later time, specifically to unobserve
*
* @method addToRegistry
* @void
Expand Down
1 change: 1 addition & 0 deletions tests/acceptance/infinity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module('Acceptance | infinity-scrollable', function(hooks) {
await settled();

assert.equal(findAll('.infinity-item').length, 20, 'after infinity has more items');
assert.equal(find('h1').textContent.trim(), '{{in-viewport}} modifier', 'has title');
});

test('works with in-viewport modifier (rAF)', async function(assert) {
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/components/dummy-artwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export default class DummyArtwork extends Component {
this.guid = guidFor(this);
}

didInsertNode(element, [instance]) {
setupInViewport(element, [instance]) {
if (instance.lazyLoad) {
// find distance of top left corner of artwork to bottom of screen. Shave off 50px so user has to scroll slightly to trigger load
window.requestAnimationFrame(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/components/my-modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default Component.extend(InViewportMixin, {

// if you do have a tagName ^^, then you can use `didInsertElement` or no-op it
// didInsertElement() {},
didInsertNode(element, [instance]) {
setupInViewport(element, [instance]) {
instance.watchElement(element);
},

Expand Down
5 changes: 5 additions & 0 deletions tests/dummy/app/controllers/infinity-built-in-modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export default class BuiltIn extends Controller {
});
}

@action
setTitle(element) {
element.textContent = '{{in-viewport}} modifier';
}

@action
didEnterViewport(/*artwork, i, element*/) {
const arr = Array.apply(null, Array(10));
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/templates/components/dummy-artwork.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div
{{did-insert this.didInsertNode this}}
{{did-insert this.setupInViewport this}}
class={{concat "dummy-artwork " this.artworkClasses}}
id={{this.guid}}
data-aspect-ratio={{this.aspectRatio}}
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/templates/components/my-modifier.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div {{did-insert this.didInsertNode this}}>
<div {{did-insert this.setupInViewport this}}>
{{yield}}
</div>
1 change: 1 addition & 0 deletions tests/dummy/app/templates/infinity-built-in-modifiers.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{{link-to "enter" (query-params direction="enter")}}
{{link-to "exit" (query-params direction="exit")}}
</nav>
<h1 {{in-viewport onEnter=(action "setTitle") viewportTolerance=this.other}}></h1>
{{#if (eq this.direction "both")}}
<ul class="infinity-container">
{{#each this.models as |artwork i|}}
Expand Down

0 comments on commit 2d8f6f7

Please sign in to comment.