Skip to content

Instantly share code, notes, and snippets.

@lennyburdette
Last active May 5, 2017 01:00
Show Gist options
  • Save lennyburdette/1709894728e7e18207025b58b1d99c1b to your computer and use it in GitHub Desktop.
Save lennyburdette/1709894728e7e18207025b58b1d99c1b to your computer and use it in GitHub Desktop.
ff select options debugging
import Ember from 'ember';
export default Ember.Component.extend({
change(e) {
this.sendAction('action', this.get(`options.${e.target.selectedIndex}`));
},
didReceiveAttrs() {
Ember.run.scheduleOnce('afterRender', () => {
this.$('select')[0].value = this.get('options').indexOf(this.get('value'));
});
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
optionsA: Ember.computed(function() {
return [{
name: 'A'
}, {
name: 'B'
}, {
name: 'C'
}, {
name: 'Create New'
}];
}),
init() {
this.set('selectedValue', this.get('optionsA.2'));
},
actions: {
update(newValue) {
if (newValue.name === 'Create New') {
const newThing = { name: 'I am new' };
const original = this.get('optionsA');
const newArray = original.slice();
newArray.splice(original.length - 1, 0, newThing);
this.set('optionsA', newArray);
this.set('selectedValue', newThing);
} else {
this.set('selectedValue', newValue);
}
}
}
});
import Ember from 'ember';
export function eq([a, b]) {
return a === b;
}
export default Ember.Helper.helper(eq);
import Ember from 'ember';
export function indexOf([haystack, needle]) {
return haystack.indexOf(needle);
}
export default Ember.Helper.helper(indexOf);
import Ember from 'ember';
export function toString([value]) {
return String(value);
}
export default Ember.Helper.helper(toString);
{{my-component options=optionsA value=selectedValue action=(action "update")}}
value: {{selectedValue.name}}
<select>
{{#each options key="name" as |option index|}}
<option value={{index}} selected={{eq option value}}>
{{option.name}} (selected: {{eq option value}})
</option>
{{/each}}
</select>
{
"version": "0.12.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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment