Skip to content

Instantly share code, notes, and snippets.

@sajt
Last active April 12, 2016 13:38
Show Gist options
  • Save sajt/06906f3062e7c6108b41fa73688d816c to your computer and use it in GitHub Desktop.
Save sajt/06906f3062e7c6108b41fa73688d816c to your computer and use it in GitHub Desktop.
BelongsTo example and question
<select onchange={{action (mut cake.factoryId) value="target.value"}} class="form-control">
<option>-- Choose one --</option>
{{#each factories as |factory|}}
<option value={{factory.id}} selected={{eq factory.id cake.factoryId}}>{{factory.name}}</option>
{{/each}}
</select>
<button type="submit" class="btn btn-default" {{action 'saveCake' cake}}">Save</button>
//app/models/cake.js
import DS from 'ember-data';
import Ember from 'ember';
export default DS.Model.extend({
name: DS.attr('string'),
factory: DS.belongsTo('factory', { async: true }),
factoryId: Ember.computed('factory', function() {
return this.get('factory.id');
}),
}),
actions: {
saveCake(cake) {
this.store.findRecord('factory', cake.factoryId).then((factory) => {
cake.set('factory', factory);
cake.save().then(() => this.transitionTo('admin.cake'),
(error) => {
Ember.Logger.debug(error);
});
});
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment