Skip to content

Instantly share code, notes, and snippets.

@runspired
Created February 24, 2016 16:52
Show Gist options
  • Save runspired/e78b0c40acc462292abb to your computer and use it in GitHub Desktop.
Save runspired/e78b0c40acc462292abb to your computer and use it in GitHub Desktop.
Example Named Yields Result, this is would be the result of AST walker transformation, but can be wired manually.
import Ember from 'ember';
export default Ember.Component.extend({
header: { title: 'HEADER' },
footer: { title: 'FOOTER' },
body: { title: 'BODY' },
slots: null,
_registerSlot(name) {
this.slots.set(name, true);
},
init() {
this._super();
this.slots = this.slots || Ember.Object.create({});
}
});
<vbox>
{{#if slots.header}}
<box fit>
{{yield (hash name='header' register=(action _registerSlot) params=(arr header 'hello' 'world'))}}
</box>
{{/if}}
<hbox>
{{yield (hash name='main' register=(action _registerSlot) params=(arr body 'goodnight' 'moon'))}}
</hbox>
{{#if slots.footer}}
<box fit>
{{yield (hash name='footer' register=(action _registerSlot) params=(arr footer 'awesome' 'you'))}}
</box>
{{/if}}
</vbox>
import Ember from 'ember';
const {
assert,
Component,
computed
} = Ember;
const component = Component.extend({
tagName: '',
name: '',
// TODO error if slot length is greater than 6
slot: null,
register: null,
shouldDisplay: computed('name', 'slot.name', function() {
return this.get('name') === this.get('slot.name');
}),
p1: computed('slot.params.[]', function() {
return this.get('slot.params').objectAt(0);
}),
p2: computed('slot.params.[]', function() {
return this.get('slot.params').objectAt(1);
}),
p3: computed('slot.params.[]', function() {
return this.get('slot.params').objectAt(2);
}),
p4: computed('slot.params.[]', function() {
return this.get('slot.params').objectAt(3);
}),
p5: computed('slot.params.[]', function() {
return this.get('slot.params').objectAt(4);
}),
init() {
this._super();
assert("You must include a name for your block", this.name);
this.slot.register(this.name);
}
});
component.reopenClass({
positionalParams: ['slot', 'name']
});
export default component;
{{#if shouldDisplay}}
{{yield p1 p2 p3 p4 p5}}
{{/if}}
import Ember from 'ember';
export function arr(params/*, hash*/) {
return Ember.A(params);
}
export default Ember.Helper.helper(arr);
<box>
{{#faked-yields as |slot|}}
{{#slot-proxy slot "main" as |info w1 w2|}}
I am the content {{info.title}} | {{w1}} {{w2}}
{{/slot-proxy}}
{{/faked-yields}}
</box>
<box>
{{#faked-yields as |slot|}}
{{#slot-proxy slot "header" as |info w1 w2|}}
I am the content {{info.title}} | {{w1}} {{w2}}
{{/slot-proxy}}
{{#slot-proxy slot "main" as |info w1 w2|}}
I am the content {{info.title}} | {{w1}} {{w2}}
{{/slot-proxy}}
{{#slot-proxy slot "footer" as |info w1 w2|}}
I am the content {{info.title}} | {{w1}} {{w2}}
{{/slot-proxy}}
{{/faked-yields}}
</box>
@runspired
Copy link
Author

  • faked-yields is an example component utilizing slots
  • slot-proxy is how we make this work, but comes with a perf overhead since all yields are now wrapped with an extra component and layer
  • arr is just a helper to make it easy to pass a lot of args together as an array (vs as a hash, may make sense to just pass p1 etc. in a hash, would likely reduce overhead, but I have not investigated)
  • example-usage is how you would compose with a component with slots in a template.

What do these look like before AST transforms?

<vbox>
  {{#if slots.header}}
    <box fit>
      {{slot 'header' header 'hello' 'world'}}
    </box>
  {{/if}}

  <hbox>
    {{slot 'main' body 'goodnight' 'moon'}}
  </hbox>

  {{#if slots.footer}}
    <box fit>
      {{slot 'footer' footer 'awesome' 'you'}}
    </box>
  {{/if}}
</vbox>
<box>
        {{#faked-yields as |info|}}
          I am the content {{info.title}}
        {{/faked-yields}}
      </box>
      <box>
        {{#faked-yields}}
          {{#slot "header" as |info|}}
            I am the content {{info.title}}
          {{/slot}}
          {{#slot "main" as |info|}}
            I am the content {{info.title}}
          {{/slot}}
          {{#slot "footer" as |info|}}
            I am the content {{info.title}}
          {{/slot}}
        {{/faked-yields}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment