Skip to content

Instantly share code, notes, and snippets.

@phillipkregg
Last active March 8, 2017 16:53
Show Gist options
  • Save phillipkregg/05d631d08628c96f34c239cfcccb166e to your computer and use it in GitHub Desktop.
Save phillipkregg/05d631d08628c96f34c239cfcccb166e to your computer and use it in GitHub Desktop.
Closure Actions
import Ember from 'ember';
export default Ember.Component.extend({
});
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
awesomeSauce() {
alert('awesome!');
},
somethinElse() {
alert('something else!');
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Closure Actions',
buttonArray: [
{
label: 'tacos',
fn: function() { this.send('awesomeSauce'); }
},
{
label: 'pizzas',
fn: function() { this.send('somethinElse') }
}
],
actions: {
buttonClick() {
alert('from controller')
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.top-level-component {
border: 1px solid blue;
padding: 20px;
}
<h1>{{appName}}</h1>
<br>
<br>
{{outlet}}
<p>If you look at each component's JS file, you'll notice that no code was necessary.</p>
<p>The action is pushed down to the component and sent back up to the controller primarily through the template itself!</p>
{{my-top-level-component buttonArray=buttonArray clickTest=(action 'buttonClick')}}
<button {{action (action buttonClick)}}>{{label}}</button>
<div class="top-level-component">
<h4>Top Level Component</h4>
<div style="border: 1px solid red; padding: 10px">
<h5>Nested Component</h5>
{{#each buttonArray as |button|}}
{{button-component label=button.label buttonClick=(action button.fn)}}
{{/each}}
</div>
</div>
{
"version": "0.10.4",
"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.7.0",
"ember-data": "2.7.0",
"ember-template-compiler": "2.7.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment