Skip to content

Instantly share code, notes, and snippets.

@krivaten
Last active February 10, 2017 16:37
Show Gist options
  • Select an option

  • Save krivaten/7b02a6bc2b778a4cccb907f0897d0731 to your computer and use it in GitHub Desktop.

Select an option

Save krivaten/7b02a6bc2b778a4cccb907f0897d0731 to your computer and use it in GitHub Desktop.
Kitten Factory
import Ember from 'ember';
export default Ember.Component.extend({
model: null
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
renderCustom() {
this.send('showCustomOutlet');
},
destroyCustom() {
this.send('destroyCustomOutlet');
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
model: null,
componentName: null
});
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
showCustomOutlet() {
console.log('Showing custom');
// Demo data
let model = [
{ name: 'Hank', description: 'Strange' },
{ name: 'Jessie', description: 'Spikey' },
{ name: 'Walt', description: 'Bald' }
];
// Set the component name to render on the custom controller
let controller = 'renderComponent';
let context = this.controllerFor(controller);
context.set('componentName', 'my-kittens');
this.render('render-component', {
into: 'application',
outlet: 'custom',
controller,
model
});
},
destroyCustomOutlet() {
console.log('Destroying custom');
this.disconnectOutlet({
outlet: 'custom',
parentView: 'application'
});
}
}
});
@import url("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css");
<div class="container">
<h1>Welcome to {{appName}}</h1>
<button class="btn btn-primary" onclick={{action 'renderCustom'}}>Show Custom</button>
<button class="btn btn-danger" onclick={{action 'destroyCustom'}}>Destroy</button>
{{outlet}}
</div>
<hr />
<div class="container">
{{outlet 'custom'}}
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Kittens</h3>
</div>
<ul class="list-group">
<li class="list-group-item">
This context for this view is: <strong>{{this._debugContainerKey}}</strong>
</li>
{{#each model as |kitten|}}
<li class="list-group-item">
<strong>{{kitten.name}}</strong>: {{kitten.description}}
</li>
{{/each}}
</ul>
</div>
<h1>Render Component Template</h1>
<p>Basically you render a template that uses the component helper to render a component. It's pretty limiting, as you have to know all the attributes you want to pass in ahead of time, but like I said, it's hacky.</p>
{{! Use that componentName in a component helper}}
{{component componentName model=model}}
{
"version": "0.11.0",
"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.10.2",
"ember-data": "2.11.0",
"ember-template-compiler": "2.10.2",
"ember-testing": "2.10.2"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment