flux icon indicating copy to clipboard operation
flux copied to clipboard

[Question] Is there any reason I couldn't use the dispatcher to update the UI too?

Open davidposin opened this issue 11 years ago • 3 comments

For various reasons, we are trying to make a strict separation of View and Model and are avoiding using the ViewController approach. So, I would prefer to not have the model emit a change event. Instead, can the UI also use the dispatcher and just wait for the store to finish? Is there something wrong with this approach? I have it working but I don't want to miss something and be surprised months down the line. Any thoughts are appreciated. I am attaching gists to the code for reference.

https://gist.github.com/Lastalas/fc4b21e35529edda00c2

It's also running at https://preview.c9.io/dposin/angular-in-react/hello-world.html

Thank you. I appreciate all input.

davidposin avatar Aug 29 '15 18:08 davidposin

One drawback would be that if you start having more stores and more actions and you aren't listening to the change events then you could be rendering more than you need to. If you handle that in each components shouldComponentUpdate though it's probably alright.

kyldvs avatar Aug 29 '15 23:08 kyldvs

Please note that what we have termed controller-views are not ViewControllers. We also call these "containers" when they only perform their listening-querying purpose and they have no visual dimension. Perhaps a better term for these more generally might be something like "listening-querying views". They are not controllers. The term "controller-view" was informed by Rails where part of a controller's purpose is to get data from models and provide it to views. It was somewhat erroneous to use the term "controller" to describe this part of Flux. Please don't get tripped up by this terminology.

I would argue that there is a very, very clean separation of view vs. model in Flux. They are completely decoupled except that the "container" does have knowledge of the store's public getters, and it will need to have some knowledge of the data structures returned by those getters. To me, this seems like a quite acceptable level knowledge of the store. Well within the "one dot rule" or Law of Demeter.

Notes on your gist:

I don't like that you're re-registering on every render. I think you would definitely want to be using Dispatcher's unregister() method so that you're not building up a bunch of redundant registrations.

Other than that, your view has knowledge of the actions it wants to listen to, which is knowledge that will stretch across the application. The view should not need to know that some button was clicked in some far away place, or what action that creates. It should only need to know that the data has changed, and so it needs to get that new data and re-render.

Gotta hand it to you though, this is a very interesting way to use the dispatcher. Dispatcher is a flexible tool, and I think there a lot of uses for it that people don't often consider. Seems like you're well aware of that and you're thinking creatively about how to use it. But for the reason I just described, and for the reason Kyle described, I would not want to use it in this way.

fisherwebdev avatar Aug 30 '15 04:08 fisherwebdev

Thank you both for your responses. I'm going to pull the registering pieces into componentWillMount so it only happens once. I'll also move back to just listening for change events.

No more referring to controller-views, promise. I like the work container better as well.

davidposin avatar Aug 30 '15 14:08 davidposin