简体   繁体   中英

ember-data fixtureAdapter with embedded hasMany

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!

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 eg 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 .

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM