Last active
December 2, 2016 13:50
-
-
Save lorcan/0746590ada06e9bbbad42780eb8e5ee3 to your computer and use it in GitHub Desktop.
Model Relationships are ObjectProxies not Models
This file contains 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'; | |
import Parent from '../models/parent'; | |
const { service } = Ember.inject; | |
export default Ember.Component.extend({ | |
store: service(), | |
parent: undefined, | |
result: undefined, | |
init() { | |
this._super(...arguments); | |
let store = this.get('store'); | |
let parent = store.createRecord('parent'); | |
let child = store.createRecord('child'); | |
parent.get('children').pushObject(child); | |
this.set('parent', parent); | |
}, | |
serializeModel(model) { | |
// Is there a single way or serializing regardless of whether this is a parent or child? | |
if (model.serialize) { | |
return `Model: ${JSON.stringify(model.serialize())}`; | |
} else if (model.content.serialize) { | |
return `ObjectProxy: ${JSON.stringify(model.content.serialize())}`; | |
} | |
}, | |
actions: { | |
serialize(model) { | |
this.set('result', this.serializeModel(model)); | |
} | |
} | |
}); |
This file contains 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({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains 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 Model from "ember-data/model"; | |
import attr from "ember-data/attr"; | |
import { belongsTo, hasMany } from "ember-data/relationships"; | |
export default Model.extend({ | |
name: 'child', | |
parent: belongsTo('parent') | |
}); |
This file contains 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 Model from "ember-data/model"; | |
import attr from "ember-data/attr"; | |
import { belongsTo, hasMany } from "ember-data/relationships"; | |
export default Model.extend({ | |
name: 'parent', | |
children: hasMany('child'), | |
}); |
This file contains 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.Route.extend({ | |
model() { | |
let model = Parent.create(); | |
model.get('children').pushObjects(Child.create()); | |
return model; | |
} | |
}); |
This file contains 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.10.6", | |
"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.9.0", | |
"ember-data": "2.9.0", | |
"ember-template-compiler": "2.9.0", | |
"ember-testing": "2.9.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment