EmbeddedRecordsMixin 没有按预期工作,我错过了什么?
EmbeddedRecordsMixin not working as expected, what am I missing?
我正在尝试在 ember 数据中使用嵌入式记录,但我认为我遗漏了一些基本的东西。
我有两个模型
app/models/video.js:
export default DS.Model.extend({
title: DS.attr('string'),
transcriptions: DS.hasMany('transcription', { embedded: 'always' })
});
app/models/transcription.js:
export default DS.Model.extend({
video: DS.belongsTo('video')
});
我还有一个自定义序列化器app/serializers/video.js:
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs:{
transcriptions: { embedded: 'always' }
},
extractSingle: function (store, type, payload, id) {
var data = payload.data;
return {
id: data._id,
title: data.Title,
transcriptions: [{ id: "1" }]
}
}
});
我希望这会导致我的视频模型被填充为转录对象数组的转录,但我却收到以下错误:
"Error while processing route: videos.show" "Assertion Failed: Ember
Data expected a number or string to represent the record(s) in the
transcriptions
relationship instead it found an object. If this is a
polymorphic relationship please specify a type
key. If this is an
embedded relationship please include the DS.EmbeddedRecordsMixin
and
specify the transcriptions
property in your serializer's attrs
object."
任何关于我在这里做错的建议都将不胜感激。
更新:解决方案是将我的自定义序列化程序修改为以下内容:
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs:{
transcriptions: { embedded: 'always' }
},
extractSingle: function (store, type, payload, id) {
var data = payload.data;
var videoPayload = {
id: data._id,
title: data.Title,
transcriptions: [{ id: "1" }]
};
return this._super(store, type, videoPayload, id);
}
}
问题是您要自己重新实现 extractSingle
。
如果你这样做,你应该打电话给 this.super
..
在 REST 序列化程序的 extractSingle
中,它调用 normalize
函数 - 这个 normalise
函数是 EmbeddedRecordsMixin 完成所有工作的地方。
因为您既没有调用 this.super
也没有手动调用 this.normalize
,所以您错过了 mixin 正在做的事情。
我正在尝试在 ember 数据中使用嵌入式记录,但我认为我遗漏了一些基本的东西。
我有两个模型
app/models/video.js:
export default DS.Model.extend({
title: DS.attr('string'),
transcriptions: DS.hasMany('transcription', { embedded: 'always' })
});
app/models/transcription.js:
export default DS.Model.extend({
video: DS.belongsTo('video')
});
我还有一个自定义序列化器app/serializers/video.js:
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs:{
transcriptions: { embedded: 'always' }
},
extractSingle: function (store, type, payload, id) {
var data = payload.data;
return {
id: data._id,
title: data.Title,
transcriptions: [{ id: "1" }]
}
}
});
我希望这会导致我的视频模型被填充为转录对象数组的转录,但我却收到以下错误:
"Error while processing route: videos.show" "Assertion Failed: Ember Data expected a number or string to represent the record(s) in the
transcriptions
relationship instead it found an object. If this is a polymorphic relationship please specify atype
key. If this is an embedded relationship please include theDS.EmbeddedRecordsMixin
and specify thetranscriptions
property in your serializer's attrs object."
任何关于我在这里做错的建议都将不胜感激。
更新:解决方案是将我的自定义序列化程序修改为以下内容:
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs:{
transcriptions: { embedded: 'always' }
},
extractSingle: function (store, type, payload, id) {
var data = payload.data;
var videoPayload = {
id: data._id,
title: data.Title,
transcriptions: [{ id: "1" }]
};
return this._super(store, type, videoPayload, id);
}
}
问题是您要自己重新实现 extractSingle
。
如果你这样做,你应该打电话给 this.super
..
在 REST 序列化程序的 extractSingle
中,它调用 normalize
函数 - 这个 normalise
函数是 EmbeddedRecordsMixin 完成所有工作的地方。
因为您既没有调用 this.super
也没有手动调用 this.normalize
,所以您错过了 mixin 正在做的事情。