Skip to content

Instantly share code, notes, and snippets.

@smfoote
Last active February 17, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smfoote/5be44e8c89497b808b76293698193bda to your computer and use it in GitHub Desktop.
Save smfoote/5be44e8c89497b808b76293698193bda to your computer and use it in GitHub Desktop.
Checked
import Ember from 'ember';
import layout from '../templates/components/toggle-pill';
const CHECKED = 'checked';
export default Ember.Component.extend({
layout,
inputId: 123,
// Component properties
checked: false,
// Lifecycle methods
didInsertElement() {
this.cacheDOMElements();
},
// Ember controlled events
click(e) {
// A click on the label will actually trigger a separate click event
// on the input. So only check for clicks on the input.
if (e.target === this.inputEl) {
e.stopPropagation();
e.preventDefault();
console.log(this.get(CHECKED));
this.get('onToggle')(!this.get(CHECKED));
}
},
// Other methods
cacheDOMElements() {
this.inputEl = this.$('input')[0];
this.labelEl = this.$('label')[0];
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
isChecked: true,
actions: {
pillToggled(checked) {
console.log('TOGGLED');
// Ember.run.later(() => {
this.set('isChecked', !this.get('isChecked'));
// });
},
toggle() {
this.set('isChecked', !this.get('isChecked'));
}
}
});
<br>
<br>
{{toggle-pill
checked=isChecked
label="hello"
onToggle=(action "pillToggled")}}
<br>
<br>
<button {{action "toggle"}}>toggle</button>
<label for="{{inputId}}">
<span class="label-text">{{label}}</span>
</label>
{{input
type="checkbox"
id=inputId
data-checked=checked
checked=checked}}
{{checked}}
{
"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