Skip to content

Instantly share code, notes, and snippets.

@adamesque
Last active January 26, 2016 22:05
Show Gist options
  • Save adamesque/9021a8e1ff7ab51b7efd to your computer and use it in GitHub Desktop.
Save adamesque/9021a8e1ff7ab51b7efd to your computer and use it in GitHub Desktop.
ToggleButton
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
isActive: false,
actions: {
toggle() {
this.toggleProperty('isActive');
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{toggle-button isActive=isActive action='toggle'}}
<br>
<br>
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
button {
background: lightgrey;
border: none;
border-radius: 4px;
font-size: 20px;
padding: 0.5em 1em;
transition: background 200ms ease-in-out;
}
.is-active button {
background: hotpink;
}
import Ember from 'ember';
const {
computed,
computed: {
reads,
},
observer,
run: {
cancel,
later,
}
} = Ember;
export default Ember.Component.extend({
classNameBindings: ['isActive'],
isActive: false,
defaultLabel: 'Label',
currentLabel: reads('defaultLabel'),
updateLabel: observer('isActive', function () {
let isActive = this.get('isActive');
if (isActive) {
this.set('currentLabel', 'ON');
} else {
this.set('currentLabel', 'OFF');
}
this._timer = later(this, 'resetLabel', 1000);
}),
resetLabel() {
this.set('currentLabel', this.get('defaultLabel'));
},
willDestroy() {
if (this._timer) {
cancel(this._timer);
}
},
click() {
this.sendAction();
}
});
<button>Button</button>
<label>{{currentLabel}}</label>
{
"version": "0.5.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment