tadasajon

tadasajon

Phoenix architecture & navigation for a single-page LiveView app

I’m developing an app that has about a dozen Postgres tables and three different contexts: an accounts context, a catalog context, and an analytics context.

I want the whole thing to be a single-page LiveView app, at least aside from a couple minor edge cases.

Following the LiveView docs, I am generating resources as follows:

mix phx.gen.live Catalog Toy toys name instructions age_range keyword:array:string store_id:references:store

Later I generate another resource as follows:

mix phx.gen.live Catalog SparePart spare_parts name instructions toy_id:references:toys 

I end up with a directory structure as follows:

lib/
├── tadasajon_web
│   ├── live
│   │   ├── live_helpers.ex
│   │   ├── modal_component.ex
│   │   ├── toy_live
│   │   │   ├── form_component.ex
│   │   │   ├── form_component.html.heex
│   │   │   ├── index.ex
│   │   │   ├── index.html.heex
│   │   │   ├── show.ex
│   │   │   └── show.html.heex
│   │   ├── spare_part_live
│   │   │   ├── form_component.ex
│   │   │   ├── form_component.html.heex
│   │   │   ├── index.ex
│   │   │   ├── index.html.heex
│   │   │   ├── show.ex
│   │   │   └── show.html.heex
│   ├── router.ex

And I am instructed to make my router.ex look as follows:

 scope "/", TadasajonWeb do
    pipe_through([:browser, :require_authenticated_user])

    live "/toys", ToyLive.Index, :index
    live "/toys/new", ToyLive.Index, :new
    live "/toys/:id/edit", ToyLive.Index, :edit
    live "/toys/:id", ToyLive.Show, :show
    live "/toys/:id/show/edit", ToyLive.Show, :edit

    live "/spare_parts", SparePartLive.Index, :index
    live "/spare_parts/new", SparePartLive.Index, :new
    live "/spare_parts/:id/edit", SparePartLive.Index, :edit
    live "/spare_parts/:id", SparePartLive.Show, :show
    live "/spare_parts/:id/show/edit", SparePartLive.Show, :edit
  end

If I examine the index.ex and show.ex files that are generated for each resource, I see that they each have a mount function and a handle_params function.

So my question is: how is this all one single-page app? It seems to me that if the user navigates to https://my-app.com/toys and then to https://my-app.com/spare_parts that they are just getting a different page reload and mounting a different LiveView each time.

I read this discussion: Concrete examples of when to use live_patch, live_redirect, push_redirect, push_patch? - #21 by cnck1387 and there appeared to be some crucial insight from Jose Valim:

An easy rule of thumb is to stick with live_redirect/2 and push_redirect/2
and use the patch helpers only in the cases where you want to minimize the
amount of data sent when navigating within the same LiveView (for example,
if you want to change the sorting of a table while also updating the URL).

But my question is: don’t I always want to minimize the amount of data sent while navigating within the same LiveView?

If I have something like a table that can be sorted, then I’ll just use a stateful live component for that – but why should I ever leave the main LiveView?

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
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
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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

Other popular topics Top

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 48475 226
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
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
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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 31307 143
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement