嵌入关系中的多态关系

时间:2014-08-18 19:06:52

标签: ember.js ember-data

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

// 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 / 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数据在反序列化注释时失败,出现以下错误:

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

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

1 个答案:

答案 0 :(得分:2)

所以@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