Ember Data EmbeddedRecordsMixin 不会保存新创建的记录
Ember Data EmbeddedRecordsMixin won't save newly created records
我正在使用 Ember Data 1.13.7 和 Ember 1.13.6 以及 ActiveModelSerializer 和 EmbeddedRecordsMixin。
我有 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
属性中的 none 并且只是 returns null
.
{post: {
attribute1: 'whatever',
attribute2: 'smth',
attribute3: 4,
comment_stuff_attributes: null}
}
我应该 saving/creating 新记录有不同的方式吗?
您应该使用 .set
和 .get
方法。不是 post.commentStuff =
,而是 post.set('commentStuff',
.
我正在使用 Ember Data 1.13.7 和 Ember 1.13.6 以及 ActiveModelSerializer 和 EmbeddedRecordsMixin。
我有 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
属性中的 none 并且只是 returns null
.
{post: {
attribute1: 'whatever',
attribute2: 'smth',
attribute3: 4,
comment_stuff_attributes: null}
}
我应该 saving/creating 新记录有不同的方式吗?
您应该使用 .set
和 .get
方法。不是 post.commentStuff =
,而是 post.set('commentStuff',
.