具有嵌入ID和侧面加载的Ember-Data和Active Model Serializer的has_many配置 [英] has_many configuration for Ember-Data and Active Model Serializers with embedded IDs and sideloading
问题描述
has_many
关系。 例如,序列化器
class PostSerializer< ActiveModel :: Serializer
embed:ids
has_many:comments
end
生成JSON
{
post:{
comment_ids:[.. 。]
}
}
但是Ember Data中的默认配置
App.Post = DS.Model.extend({
DS.hasMany('App.Comment'),
});
App.Comment = DS.Model.extend();
期望评论关联序列化为评论:[...]
没有 _ids
后缀(请参阅 Ember.js指南的REST适配器部分的关系子部分)。
我尝试过以下工作 -
class PostSerializer< ActiveModel :: Serializer
属性:comment
def comments
object.comment_ids
end
end
它有效,但添加 embed:ids,:include =>因为AMS不知道它是一个关联,所以为了启用侧面加载,真正的
将不起作用。
编辑:我正在使用 active_model_serializers(0.6.0)
gem和Ember-Data revision 11
对于ember-data 1.0.0-beta.3,我最终使用这个:
App.ApplicationSerializer = DS.ActiveModelSerializer。延伸({});
如下所述:过渡指南
非常好地工作! / p>
I know that Ember-Data is supposed to be compatible with Active Model Serializers by design, but they seem to be out of step on serializing has_many
relationships with embedded IDs.
For example, the serializer
class PostSerializer < ActiveModel::Serializer
embed :ids
has_many :comments
end
produces the JSON
{
"post": {
"comment_ids": [...]
}
}
But the default configuration in Ember Data,
App.Post = DS.Model.extend({
DS.hasMany('App.Comment'),
});
App.Comment = DS.Model.extend();
expects the comments association to be serialized as comments: [...]
without the _ids
suffix (see the relationships sub-section of the REST adapter section of the Ember.js guide).
I tried the following as a work-around:
class PostSerializer < ActiveModel::Serializer
attribute :comments
def comments
object.comment_ids
end
end
It works, but adding embed :ids, :include => true
in order to enable side-loading now does nothing since AMS doesn't know that it's an association.
Edit: I am using the active_model_serializers (0.6.0)
gem and Ember-Data revision 11
For ember-data 1.0.0-beta.3 I ended up using this:
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({});
As explained here: Transition Guide
Works very nicely!
这篇关于具有嵌入ID和侧面加载的Ember-Data和Active Model Serializer的has_many配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!