凤凰城Elixir与JaSerializer

时间:2017-12-15 23:55:07

标签: elixir phoenix-framework

ja_serializer 0.10.0,Elixir 1.4,Phoenix 1.2.1

我在尝试在视图层次结构的属性中嵌入数据时遇到了困难。

以下是我所拥有的:

defmodule MyApp.SomeView do
  use MyApp.Web, :view

  attributes ~w(details)a

  has_one :details,
      include: true,
      serializer: MyApp.DetailsView

  def details(params_here) do
    ...
  end 
  ...
end

defmodule MyApp.DetailsView do
  use MyApp.Web, :view

  attributes ~w(thing)a

  has_one :thing,
       serializer: MyApp.Details.ThingView,
       include: true
end

defmodule MyApp.Details.ThingView do
  use MyApp.Web, :view

  attributes ~w(summary)a

  has_one :summary,
      serializer: MyApp.Details.Thing.SummaryView,
      include: true
end

defmodule MyApp.Details.Thing.SummaryView do
  use MyApp.Web, :view

  attributes ~w(completed_at coordinates)a

  def coordinates(model, _) do
    %{lat: model.y, lng: model.x}
  end
end

预期产出:

"attributes": {
  "details": {
    "thing": {
      "summary": {
        "completed-at": ....
        "coordinates": {
          "lat": ...
          "lng": ...
        }
      }
    }
  }
}

实际输出:

"attributes": {
  "details": {
    "thing": {
      "summary": {
        "completed-at": ....
      }
    }
  }
}

我想知道"详细信息"功能在" MyApp.SomeView"应该进一步修改模块而不是has_one关系来获得所需的结果? has_one关系仅用于加载包含中的关系吗?

编辑:我还删除了has关系,并尝试通过

在属性函数中严格执行此操作
def details(details) do
  thing = details.thing
  summary = thing.summary
  coordinates = %{lat: summary.coordinates.x, lng: summary.coordinates.y}
  summary =  Map.merge(summary, %{coordinates: coordinates})
  thing = Map.merge(thing, %{summary: summary})

  Map.merge(details, %{thing: thing})
end  

我错过了什么?

0 个答案:

没有答案