adr

adr

Question regarding code organization/isolation best practices for a "spa-like" series of pages

Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities.

Supposing I have a section of an application that has a persistent interface with several different pages rendered in a main section of the page:

┌─────────────────┐                               
│ my_app_live.ex  │                               
├─────────────────┴──────────────────────────────┐
│                 Persistent UI                  │
├────────────────────────────────────────────────┤
│                                                │
│                                                │
│     ┌─────────────────────────────────────┐    │
│     │                                     │    │
│     │                                     │    │
│     │ Main body updating via @live_action │    │
│     │         (ie. pseudo router)         │    │
│     │                                     │    │
│     │                                     │    │
│     │                                     │    │
│     └─────────────────────────────────────┘    │
│                                                │
│                                                │
└────────────────────────────────────────────────┘

where the router might look like:

scope "/", MyAppWeb do
  pipe_through :browser

  live "/", MyAppLive, :home
  live "/my-page", MyAppLive, :my_page 
  live "/authorize", MyAppLive, :authorize
end

and I want to isolate functionality so that I don’t have one gnarly live view. Specifically supposing I have a number of handle_params that are unique to an individual subview.

What’s the best practice here? In my mind, I want to isolate the functionality of each subview within its own file. This seems very easy to do with live components, but it doesn’t really solve the issue of the callbacks unique to the subview.

Supposing the persistent UI had some kind of heavy data requirement that you wouldn’t want to trigger again and again on page navigation (and some of the data should be shared with the subviews as well), how would you go about tackling the problem while also keeping clear delineation of responsibilities (for easier maintenance, testing, etc.)

Most Liked

tfwright

tfwright

I don’t know have any great suggestions for you, but I have similar concerns and I am inclined to believe the lack of answers here means no one has thought of anything much better than what you’re doing (parent live view, subview live components). I suspect that the complexity here is not accidental, but is tied to the FE presentation layer requiring a “pseudo router.” Obviously you could delegate the param logic to their “subviews” but there’d still be some coupling. Is there a more practical problem you’re running into? I don’t think the heavy data should be retriggered by a nav change necessarily (assuming they are in the same live session)

codeanpeace

codeanpeace

Have you considered using multiple LiveViews within a live_session? That way each “subview” is just a LiveView with its own handle_params lifecycle callback. And by using live_session, you can also put shared mount logic into a shared on_mount hook for all the LiveViews in that live_session.

    live_session :default, on_mount: [MyAppWeb.SharedMount] do
      live "/", HomeLive, :home
      live "/my-page", PageLive, :my_page 
      live "/authorize", AuthLive, :authorize
    end

Naturally, there are some tradeoffs involved since each navigating within the live_session across its LiveViews will trigger mean another LiveView mount, but the websocket connection itself will be re-used across live navigation events. Plus, you could also check for no established socket via !connected? in the on_mount hook to conditionally run the shared logic that only needs to be run once at the beginning of the live_session.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement