Ember js – Hasmany关系在更新其他表后中断

我正在使用本地存储适配器的Ember.js。 更新logging时我有一个奇怪的问题。

我有一个与hasMany关系的post和评论模型:

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

这些是我的post和评论控制器:

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

创buildlogging时有许多关系正确更新。

 { "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" } } } } 

编辑post时发生问题。 编辑postlogging后,关系消失了。 我做了一些search,发现这个代码:

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

这个伎俩,它工作得很好。 但现在我有另一个问题。 如果我编辑任何其他logging,所有的id引用被replace为整个对象,如下所示:

 {"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"}}}} 

评论refrences应该是评论“:[”p31al“,”tgjtj“]像这样,但id被replace为”评论“:[ {”message“:”comment 1“},{”message“:”comment 2“ } ]

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

也许它被问到以后是否修复?

我注意到Ember中的一些东西,尤其是Ember-Data。

其中之一是处理关联时,我不得不手动重新添加关联保存并重新保存,并使用addObject到内存中的关联,就像你在这里使用的一样。 🙂

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

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

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

无论如何,下面的代码是我可能会写你的创build操作。 这可能有帮助。 我希望这样做。

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

请注意,它更简单。 一般来说,如果你正在做复杂的棘手的事情,那么就有些麻烦了,现在是回到基础的时候了,一次添加一件东西,在添加之间进行彻底的testing。 🙂

祝你好运!