【发布时间】: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