Skip to content

Instantly share code, notes, and snippets.

@simonc
Last active September 28, 2015 08:02
Show Gist options
  • Save simonc/49ef41709a222a82150d to your computer and use it in GitHub Desktop.
Save simonc/49ef41709a222a82150d to your computer and use it in GitHub Desktop.

Simple belongsTo select

This is the simplest version I could get working for a belongsTo association in a form select with EmberJS.

Ember.Select is now deprecated and should not be used.

The eq helper

It uses ember-truth-helpers to have the eq helper.

Notes

It is willingly coupled to the association name and attributes for simplicity but it could be made completely generic with a separated component and all ;-)

export default Ember.Route.extend({
model() {
return Ember.RSVP.hash({
post: this.store.find('post', 1),
categories: this.store.findAll('category')
});
},
setupController(controller, model) {
this._super(controller, model.post);
controller.set('categories', model.categories);
}
});
export default Ember.Controller.extend({
actions: {
categoryChanged(categoryId) {
let category = this.get('categories').findBy("id", categoryId);
this.get("model").set("category", category);
}
}
});
<form>
<select onchange={{action "categoryChanged" value="target.value"}}>
{{#each categories as |category|}}
<option value={{category.id}} selected={{eq category.id post.company.id)}}>
{{category.name}}
</option>
{{/each}}
</select>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment