Skip to content

Instantly share code, notes, and snippets.

@lifeart
Created June 26, 2019 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifeart/dc63a95ca9e051e173d6a4b002d0ab1a to your computer and use it in GitHub Desktop.
Save lifeart/dc63a95ca9e051e173d6a4b002d0ab1a to your computer and use it in GitHub Desktop.
ember fragment rendering
{{#performant-render}}
my - alot of dom conent, component
{{/performant-render}}
import Component from '@ember/component';
import { computed } from '@ember/object';
export default Component.extend({
internalTag: 'div',
internalClass: '',
init() {
this._super(...arguments);
this.fragment = document.createElement(this.internalTag);
if (this.internalClass) {
this.fragment.className = this.internalClass;
}
},
appendElement: computed(function() {
const selector = this.rootSelector;
if (selector) {
if (selector.startsWith('#')) {
return document.getElementById(selector.slice(1));
} else {
return document.querySelector(selector);
}
} else {
return this.element;
}
}),
addFragmentToDom: computed(function() {
const el = this.appendElement;
if (!el.contains(this.fragment)) {
el.appendChild(this.fragment);
}
return '';
})
})
{{#-in-element this.fragment}}
{{yield}}
{{/-in-element}}
{{this.addFragmentToDom}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment