You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, if I define some fields in my JSONAPI.View.fields/0 callback that are not part of the data passed when rendering, then I have to define one public function per such field.
This is alright, but it limits flexibility when we need something akin to dynamic fields. For example, imagine that for convenience I keep my data structure as
It works, but imagine what this can become if you want to do something similar with many more fields. Metaprogramming can come in handy if you generate the public functions programmatically, but it leads to code that's a bit harder to understand (especially for newcomers).
Possible Solution
A possible solution could be a c:JSONAPI.View.get_field/3 callback that takes the field name, the data, and the Plug connection. I would reimplement the example above as:
Seems reasonable. I imagine it would be beneficial to still allow individually defined functions named after fields to take precedence over get_field/3.
Currently, if I define some fields in my
JSONAPI.View.fields/0
callback that are not part of the data passed when rendering, then I have to define one public function per such field.This is alright, but it limits flexibility when we need something akin to dynamic fields. For example, imagine that for convenience I keep my data structure as
With the current capabilities, I can do this:
It works, but imagine what this can become if you want to do something similar with many more fields. Metaprogramming can come in handy if you generate the public functions programmatically, but it leads to code that's a bit harder to understand (especially for newcomers).
Possible Solution
A possible solution could be a
c:JSONAPI.View.get_field/3
callback that takes the field name, the data, and the Plug connection. I would reimplement the example above as:Did I missing an already-existing way to do this? 🙃 What are you folks' thoughts on it in case I didn't?
The text was updated successfully, but these errors were encountered: