How to make embedded hasMany relationships work with ember data

前端 未结 4 1600
既然无缘
既然无缘 2020-11-27 13:51

I can\'t get embedded hasMany to work correctly with ember data.

I have something like this

App.Post = DS.Model.extend({
          


        
相关标签:
4条回答
  • 2020-11-27 14:32

    On master, the correct API is:

    App.Adapter.map('App.Post', {
      comments: { embedded: 'always' }
    });
    

    The two possible values of embedded are:

    • load: The child records are embedded when loading, but should be saved as standalone records. In order for this to work, the child records must have an ID.
    • always: The child records are embedded when loading, and are saved embedded in the same record. This, of course, affects the dirtiness of the records (if the child record changes, the adapter will mark the parent record as dirty).

    If you don't have a custom adapter, you can call map directly on DS.RESTAdapter:

    DS.RESTAdapter.map('App.Post', {
      comments: { embedded: 'always' }
    });
    
    0 讨论(0)
  • 2020-11-27 14:44

    I have the exact same problem.

    This bug has been reported on the ember data issue tracker. The following PR adds 2 failing tests showing the problem: https://github.com/emberjs/data/pull/578

    It seems that there is no workaround right now.

    EDIT:

    sebastianseilund opened a PR 2 days ago which fixes your problem. Have a look at: https://github.com/emberjs/data/pull/629/files

    0 讨论(0)
  • 2020-11-27 14:45

    This is what worked for me (Ember 1.5.1+pre.5349ffcb, Ember Data 1.0.0-beta.7.f87cba88):

    App.Post = DS.Model.extend({
      comments: DS.hasMany('comment', { embedded: 'always' })
    });
    
    App.PostSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
      attrs: {
        comments: { embedded: 'always' }
      }
    });
    
    0 讨论(0)
  • 2020-11-27 14:51

    Adding an update to this incase others come across this post and are having a hard time figuring out what works with the current version of ember-data.

    As of Ember Data 1.0.0.beta.7, you need to override the appropriate methods on the serializer. Here's an example:

    1) Reopen the serializer (credit to this post):

    DS.RESTSerializer.reopen({
      serializeHasMany: function(record, json, relationship) {
        var hasManyRecords, key;
        key = relationship.key;
        hasManyRecords = Ember.get(record, key);
        if (hasManyRecords && relationship.options.embedded === "always") {
          json[key] = [];
          hasManyRecords.forEach(function(item, index) {
            // use includeId: true if you want the id of each model on the hasMany relationship
            json[key].push(item.serialize({ includeId: true }));
          });
        } else {
          this._super(record, json, relationship);
        }
      }
    });
    

    2) Add the embedded: 'always' option to the relationship on the model:

    App.Post = DS.Model.extend({
      comments: DS.hasMany('comment', {
        embedded: 'always'
      })
    });
    
    0 讨论(0)
提交回复
热议问题