flux icon indicating copy to clipboard operation
flux copied to clipboard

MVC misunderstandings

Open jedwards1211 opened this issue 11 years ago • 6 comments

The overview page states that "Controllers do exist in a Flux application, but they are controller-views — views often found at the top of the hierarchy that retrieve data from the stores and pass this data down to their children."

This is incorrect; a controller in MVC does not manipulate the view or know anything about view state. A controller merely updates the model when it receives events from the view.

There is so much wrong information about MVC out there; it's one of the most misunderstood concepts in programming. The most pure form of MVC is a reactive data flow just like in Flux:

  • The view listens to the model for change events. Whenever it gets one it updates itself to display the latest model data
  • The view fires an event to all registered listeners when the user performs an action
  • The controller listens to the view for action events. Whenever it gets one it changes the model state accordingly.
  • The model does not listen to anything. Whenever its state is changed, it fires a change event to all registered listeners.

model -> view -> controller -> model -> view -> controller -> etc...

Views updating models is wrong Controllers updating views is wrong Controllers listening to models is wrong Models listening to anything else is wrong

Not often covered in articles about MVC is that often something outside these three typically hooks the three together.

this.state for any React component is a ViewModel; the actual models are state inside Stores or the backend. If one View fetches data from stores and passes it down to child views, this is view logic responding to a model change event by updating ViewModels.

This is one example of how, IMHO many of Flux' criticisms of MVC show a misunderstanding of how to properly implement MVC. Please stop spreading this kind of misinformation.

jedwards1211 avatar Apr 18 '15 03:04 jedwards1211

I consider Ruby on Rails to be a fairly canonical MVC implementation. This was certainly the MVC implementation with which I was most familiar when I wrote that documentation. Rails controllers retrieve data from the model and then render the view, providing the view with that data. http://guides.rubyonrails.org/layouts_and_rendering.html#overview-how-the-pieces-fit-together

But I'd rather not debate what is and is not MVC. That's one of the most tired and endless debates on the internet, rivaling Vim vs. Emacs or semicolons in JavaScript. I'd rather just work on having some documentation about Flux that the majority of developers will be able to understand.

Facebook is not interested in criticizing MVC. I am not interested in criticizing MVC. We've simply found Flux to be a better way for us to write our applications. It's not MVC. It's almost CQRS, but not quite. But since it's not really either of these or trying to be a perfect implementation of any theoretical pattern in particular, we just call it Flux.

fisherwebdev avatar Apr 18 '15 17:04 fisherwebdev

IMO, Flux is almost the same with MVC: ActionCreator is Controller, Store is Model, Component is View. Just one thing difference, that's Dispatcher. We need the dispatcher to implement the waitFor method. And one other concept in Flux, that is one way data flow. Usually other implementations of MVC didn't use that concept, especially in JS world.

namnm avatar May 11 '15 22:05 namnm

The action creators are not controllers. Controllers act upon models. No entity acts upon the stores. Rather, they define how they will respond to actions. It is really not the same -- there is no controller imperatively orchestrating how the data layer should be updated. On May 11, 2015 3:39 PM, "soul-wos-vn" [email protected] wrote:

IMO, Flux is almost the same with MVC: ActionCreator is Controller, Store is Model, Component is View. Just one thing difference, that's Dispatcher. We need the dispatcher to implement the waitFor method.

— Reply to this email directly or view it on GitHub https://github.com/facebook/flux/issues/193#issuecomment-101067749.

fisherwebdev avatar May 11 '15 23:05 fisherwebdev

Thank for your explanation. Sorry I mean Action function, not ActionCreator. But may be I'm still misunderstood because I use some transformation in my action:

ActionDoSomething = function() {
// get data from api methods
// transform the data
// dispatch the payload data
}

Should I use the transformation before dispatching to stores, or just leave the transforming will be done later in the View rendering?

namnm avatar May 11 '15 23:05 namnm

If you need to do a transformation that is generally applicable to your entire application, I would do that in the success callback defined within a WebAPIUtils module.

But if the transformation is specific to a single store, I would do it in the store.

Likewise, if a particular transformation is only applicable to a view, or if there is a good reason to maintain the data in the store in a different, more maintainable way, then you would want to do that transformation in the view.

fisherwebdev avatar May 12 '15 15:05 fisherwebdev

Just as a historical note...

It's kind of hard to tell, but I think before the web days the controller was only considered responsible for listening to the view and updating the model. When web MVC frameworks came around they reused these terms, but the responsibilities are completely different, since there is no persistent view listening to the model: instead the controller builds a view from scratch to respond to every request. I'm pretty sure that's why there have been so many debates about the terminology.

On Mon, May 11, 2015, at 06:27 PM, Bill Fisher wrote:

The action creators are not controllers. Controllers act upon models. No

entity acts upon the stores. Rather, they define how they will respond to

actions. It is really not the same -- there is no controller imperatively

orchestrating how the data layer should be updated.

On May 11, 2015 3:39 PM, "soul-wos-vn" [email protected] wrote:

IMO, Flux is almost the same with MVC: ActionCreator is Controller, Store

is Model, Component is View. Just one thing difference, that's Dispatcher.

We need the dispatcher to implement the waitFor method.

—

Reply to this email directly or view it on GitHub

https://github.com/facebook/flux/issues/193#issuecomment-101067749.

— Reply to this email directly or view it on GitHub[1].

Links:

  1. https://github.com/facebook/flux/issues/193#issuecomment-101073545

jedwards1211 avatar May 13 '15 00:05 jedwards1211