Last active
March 5, 2019 16:04
-
-
Save magistrula/3481f6112ade39878ffcbda0c3fd76cf to your computer and use it in GitHub Desktop.
Computed BelongsTo Ids
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| export default Ember.Controller.extend({ | |
| store: Ember.inject.service(), | |
| init() { | |
| this._super(...arguments); | |
| const crust1 = this.get('store').createRecord('pizza-crust', { | |
| id: 'crust-1' | |
| }); | |
| const crust2 = this.get('store').createRecord('pizza-crust', { | |
| id: 'crust-2' | |
| }); | |
| const pizza = this.get('store').createRecord('pizza', { | |
| id: 'pizza-1', | |
| crust: crust1 | |
| }); | |
| this.setProperties({ pizza, crust1, crust2 }); | |
| }, | |
| actions: { | |
| toggleCrust() { | |
| if (this.get('pizza.crustId') === this.get('crust1.id')) { | |
| this.set('pizza.crust', this.get('crust2')); | |
| } else { | |
| this.set('pizza.crust', this.get('crust1')); | |
| } | |
| }, | |
| removeCrust() { | |
| this.set('pizza.crust', null); | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| const { computed } = Ember; | |
| export default Ember.Mixin.create({ | |
| init() { | |
| this._super(...arguments); | |
| this.eachRelationship((name, { kind }) => { | |
| if (kind === 'belongsTo') { | |
| const computedId = computed(name, function() { | |
| return this.belongsTo(name).id(); | |
| }); | |
| Ember.defineProperty(this, `${name}Id`, computedId); | |
| } | |
| }); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import DS from 'ember-data'; | |
| import WithBelongsToIds from 'app/mixins/with-belongs-to-ids'; | |
| export default DS.Model.extend(WithBelongsToIds, { | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import DS from 'ember-data'; | |
| import WithBelongsToIds from 'app/mixins/with-belongs-to-ids'; | |
| export default DS.Model.extend(WithBelongsToIds, { | |
| crust: DS.belongsTo('pizza-crust'), | |
| toppings: DS.hasMany('pizza-topping') | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "version": "0.15.1", | |
| "EmberENV": { | |
| "FEATURES": {} | |
| }, | |
| "options": { | |
| "use_pods": false, | |
| "enable-testing": false | |
| }, | |
| "dependencies": { | |
| "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
| "ember": "2.12.0", | |
| "ember-template-compiler": "2.12.0", | |
| "ember-testing": "2.12.0" | |
| }, | |
| "addons": { | |
| "ember-data": "3.4.2" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment