-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allows the view to add links #107
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ defmodule JSONAPI.Serializer do | |
meta: meta | ||
} | ||
|
||
merge_links(encoded_data, data, view, conn, remove_links?()) | ||
merge_links(encoded_data, data, view, conn, remove_links?(), true) | ||
end | ||
|
||
def encode_data(view, data, conn, query_includes) when is_list(data) do | ||
|
@@ -48,7 +48,7 @@ defmodule JSONAPI.Serializer do | |
relationships: %{} | ||
} | ||
|
||
doc = merge_links(encoded_data, data, view, conn, remove_links?()) | ||
doc = merge_links(encoded_data, data, view, conn, remove_links?(), false) | ||
|
||
doc = | ||
case view.meta(data, conn) do | ||
|
@@ -135,11 +135,19 @@ defmodule JSONAPI.Serializer do | |
merge_related_links(data, info, remove_links?()) | ||
end | ||
|
||
defp merge_links(doc, data, view, conn, false) do | ||
defp merge_links(doc, data, view, conn, false, true) do | ||
links = | ||
%{self: view.url_for(data, conn)} | ||
|> Map.merge(view.links(data, conn)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking of passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would err on the side of passing along less information than more until there is a specific need. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would you propose passing in pagination data such as a cursor, limit or offset then? I'm assuming that the controller (or whoever is serializing the data) will be in charge of that and it will be pretty opaque to the view. Am I seeing this wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my case the pagination is part of the data so I have what I need. Could you share your code where you need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's also important to remain consistent in the code and we are passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I need it. I'm still trying to figure out if I really do. Let's say I have a limit and offset I want to use, do I pass that as data in a map? Something like: render(conn, "index.json", %{
data: %{
collection: data,
pagination: %{
limit: limit,
offset: offset
}
}
}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zamith that's how the Elixir pagination libraries I'm familiar with work. You're also passing in the On a side note, are you a time traveler?! GitHub keeps sending me notifications about your notifications occurring in the future 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, that was my first approach, the problem is that the view assumes that def index(models, conn, _params, meta \\ nil), do: serialize(__MODULE__, models, conn, meta)
def render("index.json", %{data: data, conn: conn}), do: index(data, conn, conn.params) So, if anything other than an array is passed, the iteration is messed up. That's why I reached for the metadata as a way to pass in extra information. Does this make sense? Well, I'm guessing timezones are to blame :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @doomspork There is still this unresolved issue, or do you think it's not an issue at all? |
||
|
||
Map.merge(doc, %{links: links}) | ||
end | ||
|
||
defp merge_links(doc, data, view, conn, false, false) do | ||
Map.merge(doc, %{links: %{self: view.url_for(data, conn)}}) | ||
end | ||
|
||
defp merge_links(doc, _data, _view, _conn, _remove_links), do: doc | ||
defp merge_links(doc, _data, _view, _conn, _remove_links, _with_pagination), do: doc | ||
|
||
defp merge_related_links( | ||
encoded_data, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to make this configurable, pagination is optional http://jsonapi.org/format/#fetching-pagination and not everyone will want it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, but if
links
is not overridden on the view, this will have no effect, because it'll merge an empty map, right? Does it need to be more configurable than that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the suggestion here? Add an environment config like
remove_links
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zamith sorry for slow response, yes similar to remove_links