Ember数据:保存失去belongsTo关系 [英] Ember data: save loses belongsTo relationship
问题描述
我有以下问题:
具有选择字段类别的选择字段的表单。假设这个帖子有类别100.在Ember检查器中,显示如下:
A form with a select field for selecting the category of a post. Let's say the post has category 100. In Ember inspector, this is shown as follows:
category: <App.Category:ember708:100>
当我保存帖子(通过Ember Data 1.0.0 beta 2)时,类别突然变为:
When I save the post (via Ember Data 1.0.0 beta 2), the category suddenly changes to:
category: 100
并且在选择列表中不再选择该值。它被清除。
And the value is no longer selected in the select list. It is cleared.
要保存的代码:
post.save().then(
function () {
alert("Save OK");
}
)
任何想要在哪个方向我需要搜索...如果我转换到另一个页面,然后回到编辑屏幕,这些值都是正确的。数据因此在模型中仍然正确。
Any idea in which direction I need to search ... If I transition to another page and then go back to the edit screen, the values are all correct. The data is thus still correct in the model ...
推荐答案
获取最新的金丝雀 build ,这将修复 belongsTo
问题,但对于 hasMany
我尝试修改ember数据的代码,并且它工作到目前为止,
Get the latest canary build, this will fix the belongsTo
issue, but for hasMany
I tried modifying the code of ember-data, and it worked so far,
将第167行更改为
if (relationshipType === 'manyToNone'
|| relationshipType === 'manyToMany'
|| relationshipType === 'manyToOne')
更新
更好的解决方案是在序列化程序中覆盖 serializeHasMany
方法。
Better solution is override serializeHasMany
method in your serializer.
感谢 @wycats (根据github的讨论#1273 )
Thanks to @wycats (as per discussion on github #1273)
像
Deific.AppacitiveRESTSerializer = DS.RESTSerializer.extend({
//primary key is '__id' in appacitive, overriding default behaviour
primaryKey: '__id',
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] = record.get(key).mapBy('id');
// TODO support for polymorphic manyToNone and manyToMany relationships
}
}
});
Deific.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.extend({
namespace: 'service',
defaultSerializer: 'Deific/appacitiveREST'
}),
});
此时可以使用。希望这有帮助。
For time being this can be used. Hope this helps.
这篇关于Ember数据:保存失去belongsTo关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!