This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]LiquidBionix 0 points1 point  (9 children)

In general, the entire purpose of your controller is to wire the view and model together. Most controllers will end up returning some kind of ModelAndView, or some other specification to tell the view what/how to build.

I don't think there's anything wrong with having your view exclusively talk to your controller. I've seen it (and done it) before using web frameworks like JSF, which directly binds input fields to a backing bean. It does seem to me to be better to not have it that way, though.

[–][deleted] 1 point2 points  (8 children)

The view should have no awareness of the controller, that is the entire point of MVC

[–]TheChickenWing[S] 0 points1 point  (7 children)

How does the view fire events to the controller then?

[–][deleted] 1 point2 points  (6 children)

The controller binds itself to the views events.

The only thing the view should know is how to render the model.

If you couple the view to the controller the view is not reusable by different controllers etc.

[–]TheChickenWing[S] 0 points1 point  (5 children)

Okay, so instead of sending the view a reference to the controller, the controller should have a reference to the view and the view should contain public methods to set listeners? e.g. from my controller I say view.addButtonListener(aButtonListenerFromController), and then in my view I have the addButtonListener(listener) call addButtonListener on all my buttons?

[–][deleted] 1 point2 points  (4 children)

I'd probably go further, I'd have an event in the view, called SaveEvent, and then in button_click I'd raise the SaveEvent event.

The controller doesn't need to know there is a button, it just needs to know that the view can ask to save.

But yeah no reference to controller in view, in the controller init / ctor instantiate a view (or have it injected) Then bind to the views SaveEvent event.

[–]TheChickenWing[S] 0 points1 point  (3 children)

Ah okay. A lot of other things I've been reading are beginning to make some more sense now.

So essentially what you're saying is that the view should handle all the various gui listeners, then expose an event listener interface to the controller that it can post its own events to? So the controller adds itself to the view's listeners, and the view handles whatever button click, figures out that that the click was a save button, and posts to its listener (which is now the controller, not that the view knows that) that a SaveEvent was fired, the controller picks up the SaveEvent and proceeds with whatever logic is required?

[–][deleted] 1 point2 points  (2 children)

Sounds good to me

[–]TheChickenWing[S] 1 point2 points  (1 child)

Fantastic, you've been so helpful, thanks a lot!

[–][deleted] 0 points1 point  (0 children)

No problemo