StephenStephen
StephenStephen

Reputation: 114

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?

Upvotes: 4

Views: 7766

Answers (3)

Andy
Andy

Reputation: 518

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.

Upvotes: 5

sergeyz
sergeyz

Reputation: 1337

Generally view should not have any logic besides rendering. So if you handler needs to call the server ( or do something else not related to rendering ) than it should be either in a model or a controller ( depending on a framework ). If you handler needs to do some animation, that logic probably should stay in a view ( if animation only pertains to a view ).

Usually when some event is triggered on a view it calls some method on its model ( view model ) that update the state of the model. When model is done updating its state ( synced with a server ) it fires an event that its view listens. When view sees that its model got updated it re renders itself.

Controller ( or may be a router if it functions as a controller ) usually only instantiates views and models.

Actually all of that will probably depend on a framework that you choose. I use backbone.js.

Upvotes: 0

Steve Harris
Steve Harris

Reputation: 5109

I think MVC in the web can only really loosely be considered true MVC. In the case of ASP.Net MVC, your javascript events can only really be part of the view (although should be separated into js files).

If you want to separate the js events from the view entirely, you're going to have a hard time. You're better off making an educated decision on which events are really related only to the view, and which need to interact with the controller.

For example, clicking on a menu item to expand and display sub-items, is 'an event' but the controller doesn't need to know about it. But loading data based on some selection would need to post, or submit through ajax, data to the controller.

Upvotes: 2

Related Questions