加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 具有嵌入式ID和侧载的Ember-Data和Active Mode

发布时间:2020-12-16 21:55:18 所属栏目:百科 来源:网络整理
导读:我知道Ember-Data应该与设计的Active Model Serializer兼容,但它们似乎与序列化嵌入式ID的has_many关系不合时宜. 例如,序列化器 class PostSerializer ActiveModel::Serializer embed :ids has_many :commentsend 生成JSON { "post": { "comment_ids": [...]
我知道Ember-Data应该与设计的Active Model Serializer兼容,但它们似乎与序列化嵌入式ID的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后缀(参见the relationships sub-section of the REST adapter section of the Ember.js guide).

我尝试了以下作为解决方法:

class PostSerializer < ActiveModel::Serializer
  attribute :comments
  def comments
    object.comment_ids
  end
end

它有效,但添加了embed:ids,:include =>因为AMS不知道它是一个关联,所以为了启用侧载现在什么都不做.

编辑:我正在使用active_model_serializers(0.6.0)gem和Ember-Data修订版11

解决方法

对于ember-data 1.0.0-beta.3,我最终使用了这个:
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({});

如下所述:Transition Guide

工作得非常好!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读