0

I'm trying to create for a Web App an MVC Architecture with a Hierarchical Router. The goal is manage all the business logic and views with a clear pattern.

Let me do some example:

  1. Route index called
  2. Index controller called; it has the responsibility to insert the view into the page.

Now the problems are:

  1. How can I manage the required instance of model and collection or services? Where they must be stored? If I store they in the controller, how can they be accessible from external resources?

  2. How can I manage all the UI Components that have not a dedicated route (like a login overlay callble everywhere) with their business logic and required objects instances?

I'm using React with Flux pattern. One solution i've found is to create the Controller without map them directly to the routes. This way let we use Controllers both into Routes handlers and from another Controller. In this case, how can we have the Controllers accessible from anywhere?

I know it depends on usage cases, but I'm trying to found a best practice ino order to have a clearly managing pattern.

Thanks in advance!

Luca Colonnello
  • 1,416
  • 12
  • 11

1 Answers1

1

This is a pretty open-ended question, but I'll try to answer it.

  1. The way to manage dependencies in React is by passing them as props. By passing them as props or passing the data they manage as props, the receiving component won't have to know where they come from. And you push that type of decision making as far up the hierarchy as possible. That way you end up with all of that wiring centralized in one place, which is nice. If you're using react-router, you'd set a routes handler component to a component which just fetches dependencies, and renders another component passing in the needed dependencies.

  2. The most obvious way is to pass the dependencies that the login component needs to the component that renders the login component. It has the benefit of being clear and specific, but the drawback is that you need to pass those dependencies all over the place, and it's easy to miss some. There's something in React called context which lets a tree of components share a context without having to pass it around. You can find more info here: https://www.tildedave.com/2014/11/15/introduction-to-contexts-in-react-js.html. Another option is of course to couple the login component with the services it needs, by just importing it directly. The benefit is that it's easier to understand and the drawback is that you're coupling the component with its dependencies.

Anders Ekdahl
  • 22,685
  • 4
  • 70
  • 59
  • Thanks for your response! I don't like react router, it give to react more responsibility. React is the ViewModel, it can't manage routing. I'll give a glance to react context! Thanks for now! – Luca Colonnello May 18 '15 at 06:43
  • I don't really agree. React is not the ViewModel, it's the View and it needs a ViewModel/Model to render something. But React doesn't care about what Model or ViewModel implementation you use. – Anders Ekdahl May 18 '15 at 06:46
  • It's not React that has the responsibility, it's React Router. Two separate libraries. React Router just uses the familiar JSX syntax to declare (possibly nested) routes, which I find really nice. – Anders Ekdahl May 18 '15 at 06:50
  • Sorry, i like to separate all the objects. I found difficult to use react router by external configuration, and if i want to create pieces if code that dinamically add routes, this can be hard! Imho – Luca Colonnello May 18 '15 at 06:56
  • I'm not trying to persuade you to use React Router, I'm sure your concerns are valid. Just wanted to give my opinion, both for you and for other readers. – Anders Ekdahl May 18 '15 at 06:59
  • Yeah, This is important too! ;) – Luca Colonnello May 18 '15 at 07:05
  • I've read about React context. Now, the question is: what kind of object i have to create in order to have the possibility to call the login overlay everywhere in my app, without call directly its react view and manually instance all stores and dispatcher? It will mean that clicking on login button I have to call an object that create the login component for me, this is what I want. Could it be a Controller like an HMVC Architecture? – Luca Colonnello May 18 '15 at 22:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78134/discussion-between-luca-colonnello-and-anders-ekdahl). – Luca Colonnello May 18 '15 at 22:45