Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are event handlers in the view or the controller in an MVC application?

I am currently enrolled in a course on JavaScript Design patterns and I wanted to clarify the proper place for event handlers.

I noticed my professor's code included click handlers for a client side application in the view section -- my code accomplishes the same outcome, but I included click handlers in the controller.

In an MVC application, should event handlers be in the View or the Controller?

like image 604
StephenStephen Avatar asked Oct 30 '25 22:10

StephenStephen


1 Answers

In an MVC application, event-handling should definitely be placed in the view. It's a common misbelief by programmers not really aquainted with software design patterns, that event handling belongs to the controller maybe because of its name (controller = sth. that controls sth. ...). The reason is that of portability, reuse of code and modularity: imagine you want to run your application on differrent platforms: PC, web, mobile phone device. Each specific platform has its own GUI-frameworks, libraries etc., so if you place event handling stuff, which is 100% GUI-platform specific (e.g. javafx, swing, android, struts, gwt ...) in the view, you can reuse the controller and the model and only have to deal with a new custom view. The controller can be seen as a mediator between the view and the model, a middleware which is responsible for proper interaction between model and view.

like image 168
Andy Avatar answered Nov 01 '25 12:11

Andy