Skip to content

Instantly share code, notes, and snippets.

@lifeart
Created December 30, 2018 21:03
Show Gist options
  • Save lifeart/c48ae815d283d945be3c6cca6901327d to your computer and use it in GitHub Desktop.
Save lifeart/c48ae815d283d945be3c6cca6901327d to your computer and use it in GitHub Desktop.
Ember lazy-rendered component
import Component from '@ember/component';
import { later, run } from '@ember/runloop';
export default class UiLazyRendered extends Component.extend({
// anything which *must* be merged to prototype here
showContent: false,
timeout: 36,
didInsertElement() {
run('afterRender', () => {
later(this, 'toggleContent', this.timeout + Math.random() * 100);
});
},
toggleContent() {
if (this.isDestroying || this.isDestroyed) {
return;
}
this.toggleProperty('showContent');
},
tagName: ''
}) {
// normal class body definition here
}
{{#if this.showContent}}
{{yield}}
{{else}}
<div style="width: 100%; min-height: 500px;">
// some loading spinner or placeholder
</div>
{{/if}}
@lifeart
Copy link
Author

lifeart commented Jun 5, 2019

how to use it?

<LazyRendered>
  My Lazy Content
   <LazyRendered>My very lazy content</LazyRendered>
</LazyRendered>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment