sodapopcan

sodapopcan

View Models/Data Helpers/State Management

In the 15 minute twitter video, Chris broadcasts the updated Post event with the entity attached which is then simply appended to the list of Posts. This is a simple operation but it does create a little bit of duplication with the domain code—a Post is added to the conceptual list of Posts in the db, then to the literal list of Posts in the LiveView state. This duplication can get a lot hairier in more complicated scenarios, though. For example, when moving an item from list to another, there are three entities involved: the item along with the source list and destination list. This would result in having to the write that logic in both the view and the domain layers.

Is this generally the route people take? Is it viable to have a view model (or data helper) pre-emptively update the state then sending the result to the db? Is there any disadvantage to that assuming you could blow up on the db update if it fails which would re-render the LiveView in the previous state?

I would love to get some feedback with different ideas on this!

Thanks!

First Post!

LostKobrakai

LostKobrakai

This is imo a fact inherently coupled to the fact that both your db and the liveview process maintain state of a list of things, which you want both to be updated. The simplest option is letting the liveview be a plain consumer and sending it the whole new list. From there it’s optimizations to improve latency, minimize data moved around and such.

Most Liked

LostKobrakai

LostKobrakai

If you need the optimization, then feel free to duplicate the business logic and make pubsub only broadcast information about what things moved. But now you’re aware why it’s there to begin with :smiley:

Last Post!

sodapopcan

sodapopcan

Oops, I replied to myself on my note about your blog… ha!

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement