Skip to content

Instantly share code, notes, and snippets.

@samselikoff
Last active February 2, 2017 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samselikoff/64c2043978da4760259f13a69bfa4495 to your computer and use it in GitHub Desktop.
Save samselikoff/64c2043978da4760259f13a69bfa4495 to your computer and use it in GitHub Desktop.
Actions examples
<header>
<h1>Actions examples</h1>
<nav>
{{#link-to 'example-1'}}
<small>Example 1</small>
Naked functions
{{/link-to}}
{{#link-to 'example-2'}}
<small>Example 2</small>
Undefined this
{{/link-to}}
{{#link-to 'example-3'}}
<small>Example 3</small>
Function.bind
{{/link-to}}
{{#link-to 'example-4'}}
<small>Example 4</small>
\{{action}} helper
{{/link-to}}
{{#link-to 'example-5'}}
<small>Example 5</small>
actions hash
{{/link-to}}
{{#link-to 'example-6'}}
<small>Example 6</small>
action args
{{/link-to}}
{{#link-to 'example-7'}}
<small>Example 7</small>
nested actions
{{/link-to}}
{{#link-to 'example-8'}}
<small>Example 8</small>
curried args
{{/link-to}}
</nav>
</header>
<div class='padded'>
{{outlet}}
</div>
import Ember from 'ember';
export default Ember.Controller.extend({
showMessage() {
alert('You clicked me!');
}
});
<button onclick={{showMessage}}>
Click me
</button>
import Ember from 'ember';
export default Ember.Controller.extend({
counter: 0,
incrementCounter() {
this.incrementProperty('counter');
}
});
<button onclick={{incrementCounter}}>
Click me!
</button>
<p>The counter is {{counter}}</p>
import Ember from 'ember';
export default Ember.Controller.extend({
counter: 0,
init() {
// Bind our function so `this` is properly set
this.incrementCounter = this.incrementCounter.bind(this);
},
incrementCounter() {
this.incrementProperty('counter');
}
});
<button onclick={{incrementCounter}}>
Click me!
</button>
<p>The counter is {{counter}}</p>
import Ember from 'ember';
export default Ember.Controller.extend({
counter: 0,
incrementCounter() {
this.incrementProperty('counter');
}
});
<button onclick={{action incrementCounter}}>
Click me!
</button>
<p>The counter is {{counter}}</p>
import Ember from 'ember';
export default Ember.Controller.extend({
counter: 0,
actions: {
incrementCounter() {
this.incrementProperty('counter');
}
}
});
<button onclick={{action 'incrementCounter'}}>
Click me!
</button>
<p>The counter is {{counter}}</p>
import Ember from 'ember';
export default Ember.Controller.extend({
counter: 0,
actions: {
incrementCounterBy(val) {
this.set('counter', this.get('counter') + val);
}
}
});
<button {{action 'incrementCounterBy' 2}}>
Add 2
</button>
<button {{action 'incrementCounterBy' 4}}>
Add 4
</button>
<p>The counter is {{counter}}</p>
import Ember from 'ember';
export default Ember.Controller.extend({
counter: 0,
actions: {
incrementCounterBy(val) {
this.set('counter', this.get('counter') + val);
}
}
});
<button {{action (action 'incrementCounterBy' 2)}}>
Add 2
</button>
<p>The counter is {{counter}}</p>
import Ember from 'ember';
export default Ember.Controller.extend({
counter: 0,
actions: {
incrementCounterBy(...args) {
let total = args.reduce((memo, el) => memo + el);
this.set('counter', this.get('counter') + total);
}
}
});
<button {{action (action 'incrementCounterBy' 2) 4}}>
Add 2 then 4
</button>
<button {{action (action (action 'incrementCounterBy' 5) 9) 2}}>
Add 5 then 9 then 2
</button>
<p>The counter is {{counter}}</p>
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('example-1');
this.route('example-2');
this.route('example-3');
this.route('example-4');
this.route('example-5');
this.route('example-6');
this.route('example-7');
this.route('example-8');
});
export default Router;
body {
margin: 0;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
header {
background: #333;
color: white;
padding: 15px;
}
h1 {
text-align: center;
}
nav {
display: flex;
flex-direction: column;
}
nav a {
color: #999;
text-decoration: none;
margin: 5px 20px;
font-size: 18px;
}
nav a:hover,
nav a.active {
color: white;
}
nav small {
text-transform: uppercase;
font-size: 13px;
padding-right: 10px;
}
.padded {
padding: 30px;
}
{
"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