60

I am using Ember.js with local-storage-adapter. I have a weird problem while updating records.

I have a post and comments model with hasMany relationships:

App.Post = DS.Model.extend({
    title: DS.attr('string'),
    comments: DS.hasMany('comment', {
        async: true
    })
});
App.Comment = DS.Model.extend({
    message: DS.attr('string')
});

These are my post and comments controllers:

App.PostsController = Ember.ArrayController.extend({
    newTitle: '',
    actions: {
        create: function() {
            var title = this.get('newTitle');
            var post = this.store.createRecord('post', {
                title: title
            });
            this.set('newTitle', '');
            post.save();
        }
    }
});
App.CommentsController = Ember.ArrayController.extend({
    needs: "post",
    post: Ember.computed.alias("controllers.post.model"),
    newMessage: '',
    actions: {
        create: function() {
            var message = this.get('newMessage');
            var comment = this.store.createRecord('comment', {
                message: message
            });
            var post = this.get('post');
            var comments = post.get('comments');
            if (comments.get('content') == null) comments.set('content', []);
            comments.pushObject(comment);
            comment.save();
            post.save();
        }
    }
});

While creating records hasMany relations updated correctly.

{
    "App.Post": {
        "records": {
            "0v66j": {
                "id": "0v66j",
                "title": "post1",
                "comments": ["p31al", "tgjtj"]
            }
        }
    },
    "App.Comment": {
        "records": {
            "p31al": {
                "id": "p31al",
                "message": "comment 1"
            },
            "tgjtj": {
                "id": "tgjtj",
                "message": "comment 2"
            }
        }
    }
}

The problem occured while editing post. The relationships are gone after editing the post record. I did some searching and found this code:

DS.JSONSerializer.reopen({
    serializeHasMany: function(record, json, relationship) {
        var key = relationship.key;
        var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship);
        //  alert(relationshipType);
        if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany' || relationshipType === 'manyToOne') {
            json[key] = Ember.get(record, key).mapBy('id');
            // TODO support for polymorphic manyToNone and manyToMany
            // relationships
        }
    }
});

This did the trick and it worked fine. But now I have another problem. If I edit any other record, all the id references are replaced by whole object like this:

{"App.Post":{"records":{"0v66j":{"id":"0v66j","title":"post2","comments":[**{"message":"comment 1"},
{"message":"comment 2"}**]},"8nihs":{"id":"8nihs","title":"post3","comments":["b4v2b","dbki4"]}}},
"App.Comment":{"records":{"p31al":{"id":"p31al","message":"comment 1"},"tgjtj":{"id":"tgjtj","message":"comment 2"},
"b4v2b":{"id":"b4v2b","message":"comments3"},"dbki4":{"id":"dbki4",
"message":"comments4"}}}}

Comment refrences should be comments":["p31al","tgjtj"] like this. but the ids are replaced as "comments":[{"message":"comment 1"},{"message":"comment 2"}]

4

2 回答 2

1

当使用扩展 LSSerializer 的 ApplicationSerializer 时,它似乎可以工作。

也许自从被问到它已经修好了?

于 2015-04-07T17:58:35.597 回答
-1

我在使用 Ember 的过程中注意到了一些事情……尤其是 Ember-Data。

其中之一是在处理关联时,我必须手动重新添加关联保存并且必须重新保存,并使用 addObject 到内存中的关联,因为您在这里使用了一点。:)

请注意,这通常仅在我一次更新多个新对象时发生。例如,如果您的帖子是新的,而您的评论也是新的。

我有点担心在您的代码库中看到以下代码,因为它不应该在那里。您的关联中不应有 null 或非数组对象。我不确定您对适配器做了什么黑客行为以及为什么有必要,但我希望这不是原因:

  if(comments.get('content') == null)
    comments.set('content', []);

无论如何,以下代码是我可能会如何编写您的创建操作的方式。它可能会有所帮助。我希望确实如此。

create: function() {
  // get the post for association on the new comment
  var post = this.get('post');

  // get the message to store on the new comment
  var message = this.get('newMessage');

  var comment = this.store.createRecord('comment', {
    message : message,
    post : post
  });

  comment.save().then(function(savedComment) {
    post.get('comments').addObject(savedComment);
  });
}

请注意,它要简单得多。一般来说,如果你在做棘手的复杂事情,就会出现问题,是时候回到基础,一次添加一件事,在添加之间进行彻底的测试。:)

祝你好运!

于 2014-04-19T06:56:00.437 回答