【问题标题】:Polymorphic relations in embedded relationships嵌入式关系中的多态关系
【发布时间】:2014-10-11 18:46:38
【问题描述】:

我有一个 ember cli 项目,我正在尝试复制一个简单的场景,其中我有一个 Post 模型,它有许多 Comment 模型。这种关系是多态的。我有两种类型的 Body 评论和 Title 评论。

// 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')
});

我有一个 Post 模型的序列化程序

import DS from 'ember-data';
export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    comments: {
      embedded: 'always'
    }
  }
});

服务器在 GET /posts/1 上返回的 JSON 是

{
    "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 数据在反序列化 cmets 时失败,并出现以下错误:

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'));
        });
    }
});

请帮助我了解我是否做错了什么,如果是,那么解决此类需求的正确方法是什么。

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多