问题
I've been at this for a couple hours, fiddling with a number of different combinations to get this working.
LO.List = DS.Model.extend({
name: DS.attr('string'),
listItems: DS.hasMany('LO.ListItem', { embedded: true })
});
var lists = LO.store.findAll(LO.List),
firstList = lists.objectAt(0),
listItems = firstList.get('listItems'),
firstListItemId = listItems.objectAt(0).get('id');
console.log(firstListItemId) // [object Object]
http://jsfiddle.net/pjmorse/65eRS/
It seems that the 'embedded' option is not working at all (I can't seem to find any record of it in the source either, but the documentation still says to use it). I haven't been able to find out the correct option to use here (or if it's working at all). Anyone know where I can look to get this working?
Thanks for the help!
回答1:
Ember-data has removed support for embedded data. I'm almost certain that this is temporary, and would say that it should be brought back into the lib as an adapter concern.
In the mean time, feel free to use this rather ugly hasManyEmbedded shim I wrote as a stopgap for read-only embedded associations.
listItems: DS.hasManyEmbedded('LO.ListItem')
Here's a fork of your fiddle that uses this shim.
回答2:
Ember-data (version 8) does support loading embedded related objects. Thing is you have to configure the adapter's serializer to load embedded data, by calling its 'map' function. This can be done e.g. in the 'init' of the serializer, like this:
LO.Serializer = DS.Serializer.extend({
init: function(){
this._super();
this.map(LO.List, {
listItems: {
embedded: 'load'
}
});
}
});
You can find a working adaptation of your fiddle here.
回答3:
Ember returns data asynchronously, and not synchronously as your code suggests. This implies that the variable "lists" will only be updated when the data has been fetched from the persistence layer.
Solution: Binding. This can work in a variety of ways depending on what you want to do; for example, you can bind the variable "list" to a controller variable that funnels into your view. Let me know if you need further clarification.
来源:https://stackoverflow.com/questions/13538409/ember-data-fixtureadapter-with-embedded-hasmany