Skip to content

Instantly share code, notes, and snippets.

@camskene
Last active June 11, 2019 21:42
Show Gist options
  • Save camskene/dbecce3c301defdc51cf5686cd172d55 to your computer and use it in GitHub Desktop.
Save camskene/dbecce3c301defdc51cf5686cd172d55 to your computer and use it in GitHub Desktop.
Composing Patterns
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
buttons: [
{
label: 'click',
icon: 'åß',
},
{
label: 'click',
icon: 'åß',
},
],
});
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'button',
});
import Ember from 'ember';
export default Ember.Component.extend({
});
{{#each buttons as |btn|}}
<button>{{btn.icon}} {{btn.label}}</button>
{{/each}}
<!--
comparable to buttons.forEach((btn) => btn.icon)
-->
{{yield (hash
label=(component 'my-component/button' text='')
)}}
<!--
In the yielded example this could be simplified to:
`<button>{{btn.icon}} {{btn.label}}</button>` and `tagName: ''`;
-->
<!--
Notes:
* Forgot to document the `icon` argument to the yielded component (so component api and template could get out of sync).
* Which way is easier to write documentation for?
* Readability? Person would need to understand Ember's contextual components programming model; yield, hash helper, component helper, vs only needing to understand the each helper (and it's block param) which is a simple concept.
-->
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{my-component buttons=buttons}}
{{!-- this approach is like passing an options hash to a jQuery plugin --}}
+++
{{#my-component as |c|}}
{{c.label text='Click' icon='åß'}}
{{c.label text='Click' icon='åß'}}
{{/my-component}}
{{!--
Undesirable refactor of above
{{#each buttons as |btn|}}
{{#my-component as |c|}}
{{c.label text=btn.label icon=btn.icon}}
{{/my-component}}
{{/each}}
--}}
{{!--
The location of the code: a config can be a single file; the contextual component is in the middle of a (noisy?) template.
The layout of the contextual component: the component instance itself, the component template's yielded hash helper
--}}
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": true,
"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