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.

Last Post!

codeanpeace

codeanpeace

Yeah, it’s a useful analogy for sure, but it’s good to know it’s not a perfect one.

For example, a key difference is that stateless HTTP requests pass through plugs before reaching Controller actions whereas LiveView events sent within stateful connections do not pass through plugs before reaching LiveView callbacks. And this distinction introduces some important security considerations for the LiveView model around authentication and authorization.

Where Next?

Popular in Questions Top

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
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
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
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

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
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement