Ember js - 更新其他表后,有许多关系中断
Ember js - Hasmany relationships breaks after updating other tables
我正在使用带有本地存储适配器的Ember.js。我在更新记录时遇到了一个奇怪的问题。
我有一个帖子和评论模型,具有许多关系:
App.Post = DS.Model.extend({
title: DS.attr('string'),
comments: DS.hasMany('comment', {
async: true
})
});
App.Comment = DS.Model.extend({
message: DS.attr('string')
});
这些是我的帖子和评论控制器:
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();
}
}
});
创建记录时,许多关系已正确更新。
{
"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"
}
}
}
}
编辑帖子时出现问题。编辑帖子记录后,关系消失了。我做了一些搜索,找到了这段代码:
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
}
}
});
这起到了作用,而且效果很好。但现在我有另一个问题。如果我编辑任何其他记录,所有 id 引用都将替换为整个对象,如下所示:
{"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"}}}}
注释引用应该是注释":["p31al","tgjtj"]像这样。 但是 ID 被替换为"注释":[{"消息":"注释 1"},{"消息":"注释 2"}]
当使用扩展LSSerializer的应用程序序列化程序时,它似乎可以工作。
也许自从问以来它得到了修复?
我在与 Ember 的道路上注意到了一些事情......尤其是Ember-Data。
其中之一是在处理关联时,我不得不手动重新添加保存和必须重新保存的关联,并使用 addObject 到内存中的关联,就像您在这里使用的那样。 :)
请注意,这通常只发生在我一次更新多个新对象时。例如,如果您的帖子是新的,并且您的评论也是新的。
我有点担心在您的代码库中看到以下代码,因为它不需要在那里。关联中不应包含空对象或非数组对象。我不确定您对适配器做了什么黑客行为以及为什么有必要,但我希望这不是原因:
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);
});
}
请注意,它要简单得多。一般来说,如果你正在做一些棘手的复杂事情,有些事情是错误的,是时候回到基础,一次添加一件事,在添加之间彻底测试。:)
祝你好运!
相关文章:
- 气质的“nestRemoting()”有时可以'找不到关系
- 如何在Parse中创建一对多关系
- 为什么在许多浏览器中drawImage()的性能略好于createPattern()
- Ext.js从json构建模型关系的问题
- JSDoc:模块和名称空间之间的关系是什么
- 单元测试依赖关系没有被嘲笑
- 在Nodejs中为许多用户处理计时器
- FRP 中 EventStreams 的循环依赖关系
- 有许多关系创建记录 - 为什么未定义
- Ember js - 更新其他表后,有许多关系中断
- 余烬.js有许多模型关系保存
- 对于具有一对多关系的主干模型,存储“;许多“;数组中的模型,而不是Collection
- Ember排序有许多关系
- 余烬装置和有许多关系
- 序列自引用有许多关系
- Ember.js如何扁平化async的许多关系
- 如何在父模型中添加新的许多关系
- Ember数据有许多关系空结果
- 在车把模板中显示具有许多余烬关系的第一项
- 如何在javascript中动态创建具有许多父级和子级关系的Json