Skip to content

Instantly share code, notes, and snippets.

@jasonmit
Created August 6, 2014 06:29
Show Gist options
  • Save jasonmit/3124ec73d76c9b380047 to your computer and use it in GitHub Desktop.
Save jasonmit/3124ec73d76c9b380047 to your computer and use it in GitHub Desktop.
Exposing component from within the context of the yield http://jsfiddle.net/NQKvy/1233/
App = Ember.Application.create({});
var get = Ember.get, set = Ember.set, View = Ember.View;
App.FooBarComponent = Ember.Component.extend({
test: 'I am from the component!',
layout: Ember.Handlebars.compile("{{yield}}"),
_yield: function(context, options) {
var view = options.data.view,
parentView = this._parentView,
template = get(this, 'template');
if (template) {
Ember.assert("A Component must have a parent view in order to yield.", parentView);
view.appendChild(View, {
isVirtual: true,
tagName: '',
_contextView: parentView,
template: template,
context: Ember.ObjectProxy.create({
content: get(parentView, 'context'),
component: this
}),
controller: get(parentView, 'controller'),
templateData: { keywords: parentView.cloneKeywords() }
});
}
},
actions: {
whoa: function() { alert(this.elementId); }
}
});
App.IndexRoute = Ember.Route.extend({
model: function(){
return [
{firstName: 'Kris', lastName: 'Selden'},
{firstName: 'Luke', lastName: 'Melia'},
{firstName: 'Formerly Alex', lastName: 'Matchneer'}
];
}
});
<script type="text/x-handlebars" data-template-name="application">
<h1>ember-latest jsfiddle</h1>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
<h2>Index Content:</h2>
<ul>
{{#each}}
<li>
{{#foo-bar}}
{{firstName}}
{{component.test}}
<button {{action 'whoa' target='component'}}>Test</button>
{{/foo-bar}}
</li>
{{/each}}
</ul>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment