jan-swiatek

jan-swiatek

MVC vs LiveView

Hi,

I’m learning Phoenix for a while and I don’t get the differences between MVC and LiveView. When should I use which, or how to mix them properly? Can you give me some examples as well?

Most Liked

derek-zhou

derek-zhou

My only advice is don’t blend static view and LiveView until you are very comfortable with both of them. Limit your choice first; if the problem space is too vast you can easily get lost exploring.

tfwright

tfwright

This seems like a strange question, assuming you mean Model/View/Controller, since the latter is a general pattern for building software and LiveView is a very specific framework for rendering HTML server-side. The default Phoenix code structure already encourages a sort of MVC structure by encouraging you to keep business logic (Model) in contexts, and providing a template system so you can keep your mark up (View) separate from your event handlers (Controller). So really it’s only left to us as Phoenix LiveView users to try and respect the boundaries the framework has already put in place, and not put functions that generate HTML in contexts, or general business logic in specific live views, etc…

If you provide some more specific examples of things you’re confused about, maybe we can give you more specific pointers.

travisf

travisf

Based on the original question, with Phoenix, if you are following their patterns, you are already building with models and views. As @cmo mentioned, the C part of MVC is controllers, if you are using LiveViews you will have a live folder rather than a controllers folder. I’m not sure it’s harmful to think of them as being drastically different, the live folder will act as a sort of controller for data from the model and pass it to the view.

This is of course an over simplification but I think in theory it’s a good way to think of things.

As @cmo mentioned using controllers vs LiveView absolutely depends but you are not wedded to either one, you can create a controller for some pages that will have traditional HTTP request-responses and frontend JavaScript and LiveView for dynamic pages where you want to push data to and from the client without a page refresh. Personally, I find working with traditional HTTP request-responses tends to get pretty limited if I’m trying to build an interactive user experience so I tend to stick with LiveView as a default but it’s conditional on what the page is.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => "XX...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement