Skip to content

Instantly share code, notes, and snippets.

@alexlafroscia
Last active August 19, 2020 21:12
Show Gist options
  • Save alexlafroscia/bf2073de9b38a37671e19b9bb5209bac to your computer and use it in GitHub Desktop.
Save alexlafroscia/bf2073de9b38a37671e19b9bb5209bac to your computer and use it in GitHub Desktop.
Nested Tree with One Component
import Component from '@glimmer/component';
export default class extends Component {
}
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
tree = {
id: 1,
children: [
{
id: 2,
children: [
{
id: 3
}
]
},
{
id: 4
},
{
id: 5,
children: [
{ id: 6 },
{ id: 7, children: [{ id: 8 }] }
]
}
]
}
}
import { helper } from '@ember/component/helper';
export default helper(function getElementById([id]) {
return document.getElementById(id);
});
<RenderNode
@node={{this.tree}}
style="border: 1px solid black; padding: 10px;" as |node|
>
<h1>{{node.id}}</h1>
</RenderNode>
<div ...attributes id={{@node.id}}>
{{yield @node}}
{{#if @node.children}}
{{#each @node.children as |childNode|}}
{{!-- While we can't actually render each yielded element in a
tree-like structure, we _can_ force the DOM nodes to be nested
using the `in-element` helper --}}
{{#-in-element (get-element-by-id @node.id)}}
<RenderNode @node={{childNode}} ...attributes as |child|>
{{yield child}}
</RenderNode>
{{/-in-element}}
{{/each}}
{{/if}}
</div>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment