【问题标题】:Correct way to persist embedded relationships in ember-data in a Ember-cli application在 Ember-cli 应用程序的 ember-data 中持久化嵌入关系的正确方法
【发布时间】:2014-08-14 10:09:59
【问题描述】:

我面临需要将嵌入式关系持久保存到数据库中的情况。我在这个问题中描述了类似的情况。这是一个 ember-cli 项目。

我有两个模型:

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

var Post = DS.Model.extend({
    entry:          DS.attr('string'),
    comments:       DS.hasMany('comment')
});

export default Post;

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

var Comment = DS.Model.extend({
    text: DS.attr('string'),
    post: DS.belongsTo('post')
});

export default Comment;

1 序列化器: //app/serializers/post.js 从“ember-data”导入 DS;

export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    comments: {
      embedded: 'always'
    }
  }
});

1 条路线:

//app/routes/index.js
import Ember from 'ember';

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

    setupController: function(controller, model) {
        var newComment = this.store.createRecord('comment', {});
        newComment.set('text', 'xxxx comment');

        model.get('comments').pushObject(newComment);
        model.save().then(function(){
            console.log(model.toJSON());
            comments = model.get('comments');
            comments.forEach(function(comment){
                console.log("Comment: " + comment.get('text'));
                console.log("Comment id: " + comment.get('id'));
            });
       });
    }
});

所以,服务器返回的model钩子中的GET调用:

// GET /posts/1
{
    "posts": {
        "id": "1",
        "entry": "This is first post",
        "comments": [
            {
                "id": "1",
                "post": "1",
                "text": "This is the first comment on first post"
            },
            {
                "id": "2",
                "post": "1",
                "text": "This is the second comment on first post"
            }
        ]
    }
}

当在 setupController 钩子中时,我在帖子中添加一条新评论并保存它,它实际上发送了一个带有以下正文的 PUT 请求:

// PUT /posts/1 -- Request
{
    "posts": {
        "id": "1",
        "entry": "This is first post",
        "comments": [
            {
                "id": "1",
                "post": "1",
                "text": "This is the first comment on first post"
            },
            {
                "id": "2",
                "post": "1",
                "text": "This is the second comment on first post"
            },
            {
                "post": "1",
                "text": "xxxx comment"
            }
        ]
    }
}

服务器返回以下输出:

// PUT /posts/1 -- Response
{
    "posts": {
        "id": "1",
        "entry": "This is first post",
        "comments": [
            {
                "id": "1",
                "post": "1",
                "text": "This is the first comment on first post"
            },
            {
                "id": "2",
                "post": "1",
                "text": "This is the second comment on first post"
            },
            {
                "id": "3",
                "post": "1",
                "text": "xxxx comment"
            }
        ]
    }
}

但现在在控制台日志中我得到以下输出:

Comment: This is the first comment on first post
Comment id: 1
Comment: This is the second comment on first post
Comment id: 2
Comment: xxxx comment
Comment id: 3
Comment: xxxx comment
Comment id: null

为什么用id 返回的新评论被添加到帖子的 cmets 中而不是替换评论? 我做错了什么还是需要为此添加其他内容?

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    Ember Data 无法准确识别用户尝试保存的记录与其他用户尝试保存的记录之间的区别。

    它可以安全地知道的是,一条带有新 id 的新记录回来了(因为之前的记录上没有唯一标识符,并且您没有指定保存该确切记录)。

    在非多用户世界中,它可以假设新记录应该替换现有记录,但嵌入式记录的东西还没有那么聪明。

    1。保存后删除记录(因为你知道它会被欺骗,hacky)

    var comments = model.get('comments');
    comments.pushObject(newComment);
    model.save().then(function(){
      comments.popObject(newComment);
      newComment.deleteRecord(); // not really necessary
      ...
    });
    

    2。从评论的角度保存记录(最便宜和最干净,对你来说可能是一些额外的服务器端逻辑)

    newComment.save();
    

    【讨论】:

    • 感谢@kingpin2k,这行得通。在实际场景中,父模型将有许多嵌套的配置,因此当用户保存父记录时,必须保存整个文档(包含所有嵌套配置)。所以,对我来说保存评论(在这种情况下)是没有意义的。
    猜你喜欢
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2015-02-21
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多