【问题标题】:Ember Data EmbeddedRecordsMixin won't save newly created recordsEmber Data EmbeddedRecordsMixin 不会保存新创建的记录
【发布时间】:2015-08-26 18:36:57
【问题描述】:

我正在使用带有 ActiveModelSerializer 和 EmbeddedRecordsMixin 的 Ember Data 1.13.7 和 Ember 1.13.6。

我有 2 个模型:

// models/post.js
export default DS.Model.extend({
  //bunch of attrs
  commentStuff: DS.belongsTo('commentStuff', {async:true})
}

//models/comment-stuff.js
export default DS.Model.extend({
  //bunch of attrs
  post: DS.belongsTo('post', {async: true))
}

在我的序列化程序中我有

export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  isNewSerializerAPI: true,

  attrs: {
    commentStuff: {serialize: 'records', deserialize: 'ids'}
  },

  keyForAttribute(attr){
    if(attr === 'commentStuff'){
      return 'comment_stuff_attributes';
    } else {
      return this._super(attr);
    }
  }

});

当我编辑现有记录然后 post.save() 但当我创建新记录时,这非常有效:

var post = this.store.createRecord('post');
post.commentStuff = this.store.createRecord('commentStuff');

然后填写各自的所有属性。在 post.save() 上发送到服务器的 json 不显示任何 commentStuff 属性,只返回 null

{post: {
  attribute1: 'whatever',
  attribute2: 'smth',
  attribute3: 4,
  comment_stuff_attributes: null}
}

我应该以不同的方式保存/创建新记录吗?

【问题讨论】:

    标签: ember.js ember-data active-model-serializers


    【解决方案1】:

    您应该使用.set.get 方法。不是post.commentStuff =,而是post.set('commentStuff',

    【讨论】:

    • 似乎知道相关记录,所以我认为这部分可能不是问题。我松了一口气!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多