gpt4 book ai didi

ember.js - 嵌入关系中的多态关系

转载 作者:行者123 更新时间:2023-12-01 03:48:02 25 4
gpt4 key购买 nike

我有一个 ember cli 项目,我正在尝试复制一个简单的场景,其中我有一个 发帖 型号有很多评论 模型。这种关系是多态的。我有两种类型 正文 评论和 标题评论。

// app/models/post.js
import DS from 'ember-data';

export default DS.Model.extend({
entry: DS.attr('string'),
comments: DS.hasMany('comment', {polymorphic: true})
});


// app/models/comment.js
import DS from 'ember-data';

export default DS.Model.extend({
text: DS.attr('string'),
post: DS.belongsTo('post')
});

// app/models/body.js
import DS from 'ember-data';
import Comment from './comment';

export default Comment.extend({
body: DS.attr('string')
});

// app/models/title.js
import DS from 'ember-data';
import Comment from './comment';

export default Comment.extend({
title: DS.attr('string')
});

我有一个 的序列化程序发帖 模型
import DS from 'ember-data';
export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
comments: {
embedded: 'always'
}
}
});

服务器在 GET 上返回的 JSON /posts/1
{
"posts": {
"id": "1",
"entry": "This is first post",
"comments": [
{
"id": "1",
"post": "1",
"type": "body",
"text": "This is the first comment on first post",
"body": "This is a body comment"
},
{
"id": "2",
"post": "1",
"type": "title",
"text": "This is the second comment on first post",
"title": "This is a title comment"
}
]
}
}

但是 Ember 数据在反序列化注释时失败,出现以下错误:
Error while processing route: index Cannot read property 'typeKey' of undefined TypeError: Cannot read property 'typeKey' of undefined
at Ember.Object.extend.modelFor (http://localhost:4200/assets/vendor.js:71619:22)
at Ember.Object.extend.recordForId (http://localhost:4200/assets/vendor.js:71074:25)
at deserializeRecordId (http://localhost:4200/assets/vendor.js:72099:27)
at deserializeRecordIds (http://localhost:4200/assets/vendor.js:72116:9)
at http://localhost:4200/assets/vendor.js:72081:11
at http://localhost:4200/assets/vendor.js:70135:20
at http://localhost:4200/assets/vendor.js:17687:20
at Object.OrderedSet.forEach (http://localhost:4200/assets/vendor.js:17530:14)
at Object.Map.forEach (http://localhost:4200/assets/vendor.js:17685:14)
at Function.Model.reopenClass.eachRelationship (http://localhost:4200/assets/vendor.js:70134:42)

执行以下代码时会发生这种情况:
import Ember from 'ember';

export default Ember.Route.extend({
model: function() {
return this.store.find('post', 1);
},

setupController: function(controller, model) {
console.log("Post entry: " + model.get('entry'));
var comments = model.get('comments');
comments.forEach(function(comment){
console.log("Comment: " + comment.get('text'));
console.log(typeof comment);
//console.log("Comment Body " + comment.get('body'));
//console.log("Comment Title " + comment.get('title'));
});
}
});

请帮助我理解我是否做错了什么,如果是,那么解决这样的要求的正确方法是什么。

最佳答案

所以@Dipayan 你不一定在这里做错了什么,Ember Data 还不支持 HasMany Embedded Polymorphic 关系。 Polymorphic embedded hasMany relationship .它会给 TypeError: Cannot read property 'typeKey' of undefined如果您尝试使用它,这正是您所得到的。

如果您可以控制服务器处理数据的方式,您可以保持多态,但丢失嵌入式,而使用 side-loading .有关以这种方式使用多态的示例和很好的解释,请查看 Topal's Guide to Ember Data .

关于ember.js - 嵌入关系中的多态关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25370351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com