Skip to content

Instantly share code, notes, and snippets.

@ultimatemonty
Created October 3, 2016 12:51
Show Gist options
  • Save ultimatemonty/399e7db4651619e059a35d4f29e4a89c to your computer and use it in GitHub Desktop.
Save ultimatemonty/399e7db4651619e059a35d4f29e4a89c to your computer and use it in GitHub Desktop.
Contextual Components
import Ember from 'ember';
export default Ember.Component.extend({
model: null,
parent: null,
init() {
this._super(...arguments);
if (this.get('parent')) {
this.get('parent').register(this);
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
children: null,
init() {
this._super(...arguments);
this.set('children', []);
},
registerWithParent(child) {
Ember.run.later(function() {
this.get('children').addObject(child);
}, 1000);
}
});
import Ember from 'ember';
const childObject = Ember.Object.extend({
name: null
});
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
models: null,
init() {
this._super(...arguments);
this.set('models', [
childObject.create({ name: 'Child 1' }),
childObject.create({ name: 'Child 2' }),
childObject.create({ name: 'Child 3' })
]);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{parent-component children=models}}
<br>
<br>
<h2>Number of children: {{children.length}}</h2>
{{#each models as |model|}}
{{child-component model=model parent=this}}
{{/each}}
{
"version": "0.10.5",
"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.8.0",
"ember-data": "2.8.0",
"ember-template-compiler": "2.8.0",
"ember-testing": "2.8.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment