App.Comment = DS.Model.extend({ content: DS.attr('string'), post: DS.belongsTo('App.Post') }); App.Post = DS.Model.extend({ body: DS.attr('string'), comments: DS.hasMany('App.Comment', { embedded: true } ) });
Full code: ** http://jsfiddle.net/v2t67/ **
After reading the association test from the source, I found a way to do this. The test for the association is here: ** https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/associations_test.js **
Still adhering to the two models above in our example, we can request the json data of the built-in model (comment model) by doing:
**Approach 1** query = App.store.find(App.Post, 1) query.get('comments').objectAt(0).toJSON()
When doing further checks, I found that the above will not work if you want to use the json of the parent model (Post model). Here is how I find json for this:
**Approach 2** query = App.store.find(App.Post) query.objectAt(0).toJSON()
You get TypeError: Object does not have a method of 'objectAt' if you try to get json data for Post Model using approach 1, and you get TypeError: you can not call the method 'objectAt' from undefined if you try to get json data for embedded model using approach 2.
I will update this if I learn something new.
**UPDATE**
You can get approach 2 to return the json data for the inline model without errors by passing {association: true} to the toJSON () function, as shown below:
query = App.store.find(App.Post) query.objectAt(0).toJSON({associations: true})
source share