elijahbrandyb

elijahbrandyb

Passing Callbacks to Live Components to increase usability: yes or no?

I am in the process of upgrading our project to LiveView 0.18. An interesting use case that has popped up is setting up a reusable live component for record forms that handle the Ecto changeset change validation and other boilerplate logic. This reduces the boilerplate by about 80-90%, but in my current implementation requires passing in function captures. The question is, is it an acceptable practice to “select” business logic in a template/component, or should I refactor this to require more boilerplate (probably closer to about 60% reduction) but have business logic purely handled in the parent live view’s handle_event? I could use handle_info as well, but that means that the logic is processed outside of the live event which breaks things like error handling and phx-disable-with.

Here is an implementation of my current approach:

<.live_component module={RecordForm} :let={f} for={@user} change={&Accounts.change_user/2} update={&Accounts.update_user/2}>
...
</.live_component>

So, Live View or Templates?

  • Keep all references to business logic in the Live View
  • References to business logic in a template can be used in moderation
0 voters

First Post!

D4no0

D4no0

I would say go with whatever works for you. I used to make components like these too, the danger with such things is when your component starts to do too many things, you start to lose track of what is going on, so for small scale I always tend to make small custom components, if there are too many of the same kind, refactor to one that is more generic.

Where Next?

Popular in Discussions Top

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement