Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danmeyers/a5a49519c1c53c0ec267d15a24853195 to your computer and use it in GitHub Desktop.
Save danmeyers/a5a49519c1c53c0ec267d15a24853195 to your computer and use it in GitHub Desktop.
sq-option-set
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
setValue(newValue) {
this.sendAction('valueChanges', newValue);
},
toggleValue(newValue, isSelected) {
if (isSelected) {
this.send('addValue', newValue);
} else {
this.send('removeValue', newValue);
}
},
addValue(newValue) {
this.sendAction('valueChanges', [...this.get('value'), newValue]);
},
removeValue(newValue) {
this.sendAction('valueChanges', this.get('value').without(newValue));
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
someArray: Ember.computed(function() {
return "red orange yellow green blue indigo violet".w()
}),
currentValue: "red",
multipleValues: Ember.computed(function() {
return "orange green".w();
})
});
import Ember from 'ember';
export function eq([a, b]) {
return a === b;
}
export default Ember.Helper.helper(eq);
import Ember from 'ember';
export function includes([needle, haystack]) {
return haystack.includes(needle);
}
export default Ember.Helper.helper(includes);
import Ember from 'ember';
export function isArray([array]) {
return Ember.isArray(array);
}
export default Ember.Helper.helper(isArray);
{{#sq-option-set options=someArray value=currentValue valueChanges=(action (mut currentValue)) as |option|}}
<label><input type="radio" checked={{option.isSelected}} onchange={{action option.setValue}} /> {{option.value}}</label>
{{/sq-option-set}}
{{currentValue}}
<hr>
{{#sq-option-set options=someArray value=multipleValues valueChanges=(action (mut multipleValues)) as |option|}}
<label><input type="checkbox" checked={{option.isSelected}} onchange={{action option.toggleValue value="target.checked"}} /> {{option.value}}</label>
{{/sq-option-set}}
{{multipleValues}}
{{#each options as |option|}}
{{yield (hash
isSelected=(if (is-array value) (includes option value) (eq option value))
value=option
setValue=(action "setValue" option)
addValue=(action "addValue" option)
removeValue=(action "removeValue" option)
toggleValue=(action "toggleValue" option))}}
{{/each}}
{
"version": "0.11.1",
"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.11.0",
"ember-data": "2.11.0",
"ember-template-compiler": "2.11.0",
"ember-testing": "2.11.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment