Skip to content

Instantly share code, notes, and snippets.

@pswai
Last active August 18, 2022 17:32
Show Gist options
  • Save pswai/2215147936b882f45c878cdee91efe1f to your computer and use it in GitHub Desktop.
Save pswai/2215147936b882f45c878cdee91efe1f to your computer and use it in GitHub Desktop.
JS/HBS Hierarchy in Components
import Ember from 'ember';
export default Ember.Component.extend({
// Count here demonstrates that this component provides data
count: 0,
dataToBeYielded: Ember.computed('count', function() {
return `Hello from bar! Count is ${this.get('count')}.`;
}),
actions: {
incrementCount() {
this.set('count', this.get('count') + 1);
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
// We have no access to `data` and `incrementCount` yielded by bar-comp
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
{{yield dataToBeYielded (action 'incrementCount')}}
{{#bar-comp as |data incrementCount|}}
<div>
In <code>foo-comp.hbs</code>: {{data}}
</div>
<div>
<button type="button" {{action incrementCount}}>Increment Count</button>
</div>
{{/bar-comp}}
<div style="margin-top: 20px">
Several things to note here:
<ul>
<li><code>data</code> and <code>incrementCount</code> are not available here. Data: {{data}} (It should be blank)</li>
<li><code>foo-comp.js</code> does not have access to things yielded by <code>bar-comp</code></li>
<li>To overcome the limitation of <code>yield</code>, <a href="https://ember-twiddle.com/d7b42d2c6d480dda2972cef08563b4e5">HOC is a better option (Right click to open in new tab)</a></li>
</ul>
</div>
{
"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