保存模型会打破一对多的关系 [英] Saving a model breaks one to many relationships
问题描述
当我保存一个Parent对象(有很多Child对象)时,关系看起来就被破坏了。 Ember docs 对此没有提及。
When I save a Parent object (which has many Child objects), the relationship is seemingly broken. The Ember docs are silent on this issue.
我需要做什么才能使Parent对象仍然显示Child对象?
What do I need to do so that the Parent object still shows the Child objects?
运行此操作时,我希望父名称从泰勒改为马特,而本的孩子将保持不变,并保持在页面上。目前,该操作将从父级中删除该子项。
When this action is run, I expect the Parent name will be changed from "Taylor" to "Matt", and the Child, "Ben", will be unchanged and remain on the page. Currently, the action removes the Child from the Parent.
saveParent: function() {
this.store.find('parent', 1).then(function (parent) {
parent.set('name', 'Matt');
parent.save();
});
}
jsbin示例: http://jsbin.com/zodorule/8/
推荐答案
p>这是JSONSerializer的一个问题,并且有很多关系。你可以查看这个'固定'的jsbin: http://jsbin.com/zodorule/13
This is an issue with the JSONSerializer and hasMany relationships. You can check this 'fixed' jsbin: http://jsbin.com/zodorule/13
我把它添加到你的代码,它的工作原理:
I added this to your code and it works:
DS.JSONSerializer.reopen({
serializeHasMany : function(record, json, relationship) {
var key = relationship.key;
var relationshipType = DS.RelationshipChange.determineRelationshipType(
record.constructor, relationship);
if (relationshipType === 'manyToNone'
|| relationshipType === 'manyToMany'
|| relationshipType === 'manyToOne') {
json[key] = Ember.get(record, key).mapBy('id');
// TODO support for polymorphic manyToNone and manyToMany
// relationships
}
}
});
这篇关于保存模型会打破一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!