knoebber

knoebber

Anonymous functions as component attributes?

I have a table component that can apply custom sorting. Each <th> element has a patch link that leads to a new URL, E.G, http://localhost:4000/runs?order=desc&order_by=miles or http://localhost:4000/runs?order=asc&order_by=miles

I accomplished this by passing the table component an anonymous function that generates a route given a map of params, which is then passed to an internal lib function that generates the filter map. Here’s an example of how I use the component in a LiveView:

<.table 
   rows={@runs} 
   get_route={fn filter -> ~p"/runs?#{filter}" end} # anonymous function that makes next order
   filter={@filter} #  map of URL params - <.th> uses this to render the current order
  >
  <:col :let={run} label="Miles" order_col="miles">
    <.link navigate={~p"/runs/#{run.id}"}>
      <%= run.miles %>
     </.link>
  </:col>
  ....
</.table>

Is there a better way to do this? Does it make any difference if I instead declare a named get_route function in the module? Here’s an abbreviated version of my component:

  defp get_next_sort_link(get_route, filter, order_col) do
    filter
    |> Filter.apply_sort(order_col) # returns a map
    |> get_route.() # applies the anonymous function
  end

  attr :filter, :map, default: %{}
  attr :rows, :list, required: true
  attr :get_route, :any, default: nil # is there a better type than 'any' ?

  slot :col do
    attr :label, :string, required: true
    attr :order_col, :string
  end

  def table(assigns) do
    <table>
      <thead>
        <tr>
          <%= for col <- @col do %>
            <.th
              order={Filter.current_order(@filter, Map.get(col, :order_col, nil))}
              next_sort_link={
                get_next_sort_link(
                  @get_route,
                  @filter,
                  Map.get(col, :order_col, nil)
                )
              }
            >
              <%= col.label %>
            </.th>
          <% end %>
 ...
end

This all works and doesn’t cause any problems as far as I can tell, but I’m wondering if there is a better way. Thx!

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement