Skip to content

Instantly share code, notes, and snippets.

@ASH-Anthony
Last active March 8, 2019 17:53
Show Gist options
  • Save ASH-Anthony/e2edfc66bd821b0d19a9b160637958d1 to your computer and use it in GitHub Desktop.
Save ASH-Anthony/e2edfc66bd821b0d19a9b160637958d1 to your computer and use it in GitHub Desktop.
Child vs Parent Render
import Ember from 'ember';
export default Ember.Component.extend({
classNames:['child-component'],
init(){
this._super(...arguments)
console.log(`hello from ${this}`)
},
});
import Ember from 'ember';
export default Ember.Component.extend({
classNames:['child-component'],
init(){
this._super(...arguments)
console.log(`hello from ${this}`)
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
showChild:false
});
<h1>Conditional Rendering in Child vs Parent</h1>
<p>"child-bool" will always render and lifecylce methods even get called when you don't want them because the parent instantiated it. But "parent-bool" does not until the parent boolean toggles and instantiates the component render. You can see the effect of this by:</p>
<ol>
<li>Look at the console and see that child-bool console logs are printed even when the component isn't visible</li>
<li>Open Elements in devtools and see that the class "child-component" is in the DOM 1 time when child compoents should not be rendered and twice when they should be. Child-bool is always rendered! </li>
</ol>
<h2>With conditional in component</h2>
<ChildBool
@show={{this.showChild}}
/>
<h2>With conditional in parent</h2>
{{#if this.showChild}}
<ParentBool/>
{{/if}}
<label for='c'>Show Child Components</label>
{{input
id='c'
type='checkbox'
checked=showChild
onChange=(mut showChild)
}}
{{outlet}}
{{#if this.show}}
<p data-experiment="child">child component</p>
{{this.someComputed}}
{{/if}}
<p data-experiment="child">child component</p>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment