Skip to content

Instantly share code, notes, and snippets.

@k-fish
Last active July 25, 2019 14:08
Show Gist options
  • Save k-fish/5464f3b70c2a581d69c4e6561cbfa23a to your computer and use it in GitHub Desktop.
Save k-fish/5464f3b70c2a581d69c4e6561cbfa23a to your computer and use it in GitHub Desktop.
Contextual component examples
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['green']
});
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['red'],
foo: 'Parent component context',
firstContext: 'First context',
secondContext: 'Second context',
thirdContext: 'Third context',
});
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['red'],
foo: 'Parent component context'
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
foo: 'Application controller context',
bar: 'Application controller context'
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
div {
padding: 10px;
margin: 10px;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
.yellow {
background-color: yellow;
}
.cyan {
background-color: cyan;
}
<h1>1. Parent component by itself</h1>
{{parent-component}}
<h1>2. Child component by itself</h1>
{{child-component}}
<h1>3. Parent component containing hbs (no context)</h1>
{{#parent-component}}
<div class="cyan">
Some hbs code
</div>
{{/parent-component}}
<h1>4. Parent component containing hbs (controller context)</h1>
{{#parent-component}}
<div class="cyan">
Some hbs code<br/>
Foo: {{foo}}
</div>
{{/parent-component}}
<h1>5. Parent component containing hbs (parent component context)</h1>
{{#parent-component as |foo|}}
<div class="cyan">
Some hbs code<br/>
Foo: {{foo}}<br/>
Bar: {{bar}}
</div>
{{/parent-component}}
<h1>6. Parent component containing child component (no context)</h1>
{{#parent-component}}
{{child-component}}
{{/parent-component}}
<h1>7. Parent component containing child component (controller context)</h1>
{{#parent-component}}
{{child-component foo=foo}}
{{/parent-component}}
<h1>8. Parent component containing child component (parent component context)</h1>
{{#parent-component as |foo|}}
{{child-component foo=foo}}
{{/parent-component}}
<h1>9. Yield being used multiple times</h1>
{{#parent-component-multiple-yield}}
{{child-component}}
{{/parent-component-multiple-yield}}
<h1>10. Yielding a child component w/o override</h1>
{{#parent-component as |notUsed api|}}
{{api.childComponent}}
{{/parent-component}}
<h1>11. Yielding a child component w/ override</h1>
{{#parent-component as |notUsed api|}}
{{api.childComponent foo='Override!'}}
{{/parent-component}}
Some context in the child component <br/>
{{#if foo}}
<strong>Context received:</strong>{{foo}}
{{/if}}
Some context in the parent component
<div class="yellow">
First Yield
{{yield firstContext}}
</div>
<div class="yellow">
Second Yield
{{yield secondContext}}
</div>
<div class="yellow">
Third Yield
{{yield thirdContext}}
</div>
<div class="yellow">
Fourth Yield (reusing first context)
{{yield firstContext}}
</div>
Some context in the parent context
{{yield foo}}
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment