Help with JSON generation of related models

First-time poster. Phoenix newbie and JSON newbie confusion:

I’m trying to wrap my head around around single page applications and I’m trying seemingly simple stuff with Emberjs and Phoenix as the JSON backend using ja_serializer. I’ve got something simple working, but don’t really understand why it works or how to expand on it.

Here is my setup at this point:

defp deps do
    [{:phoenix, "~> 1.1.4"},
     {:mariaex, ">= 0.0.0"},
     {:phoenix_ecto, "~> 2.0"},
     {:gettext, "~> 0.9"},
     {:ja_serializer, "~> 0.11.1"},
     {:cors_plug, "~> 0.1.3"},
     {:cowboy, "~> 1.0"}]
  end

Suppose I have a “meeting” that has many “agendaitems”.

defmodule Meetings.Meeting do
  use Meetings.Web, :model

  schema "meetings" do
    field :attendees, :string
    field :owner, :string
    field :keywords, :string
    field :meeting_date, Ecto.DateTime
    has_many :agendaitems, Meetings.Agendaitem

    timestamps
  end
end
defmodule Meetings.Agendaitem do
  use Meetings.Web, :model

  schema "agendaitems" do
    field :itemtext, :string
    belongs_to :meeting, Meetings.Meeting

    timestamps
  end
end

Based on what I’ve read with jaserializer docs and some blogs, I created a view:

defmodule Meetings.MeetingView do
  use Meetings.Web, :view
  use JaSerializer.PhoenixView

attributes [:id, :attendees, :inserted_at, :updated_at]
has_many :agendaitems,
    serializer: Meetings.AgendaitemSerializer,
    type: "agendaitem",
    include: true,
    links: [
      related: "/meeting/:id/agendaitems",
    ]

def agendaitems(model,conn) do
  case model.agendaitems do
    %Ecto.Association.NotLoaded{} ->
      model
      |> Ecto.Model.assoc(:agendaitems)
      |> Meetings.Repo.all
      other -> other
  end
end
end
defmodule Meetings.AgendaitemSerializer do
  use Meetings.Web, :view
  use JaSerializer.PhoenixView

attributes [:id, :itemtext, :inserted_at, :updated_at, :meeting_id]
end

My questions are:

  1. I’m not sure why in the view I need to include the “def agendaitems(model, conn)…” method. I found something like it in a SO thread, and it appears to be how I can get the agenditems to be included in the resultant json data. I guess I thought having “include: true” on the has_many relation of the model would take care of loading and serializing the agendaitems. I hope to expand this scenario so that agendaitems themselves can have “notes” associated with them. Does every has_many or belong_to association needs this sort of Ecto.Association.NotLoaded handler? Or am I missing the “easy way” to do this?

  2. in my mind, going to the route “/meetings” ideally would just return the basic meeting information for all the meetings in the database – id, attendees, meeting date but would not need to return all the agenda items for all the meetings at that point. Whereas going to the route “/meeting/1” would return the basic information about that specific meeting AND the agendaitems for that meeting. However, with the given model, view and serializer, I’m getting the agenda item details for all the meetings when I visit “/meetings” as well as when I visit “/meeting/1”. I assume that is due to the “include: true” on the has_many attribute of the view. So if I wanted another view for use with “/meetings” that did NOT include the agenda items in the JSON response, and a view that did (for “/meeting/1”), how do I get phoenix or jaserializer to switch between the two? It looks like “MeetingView” is applied to both cases.

  3. Does anyone have any pointers to tutorials that may help with more complex data scenarios than simple todo lists?

Thanks in advance for your time and patience.
Rogelio