barry
barry

Reputation: 249

Javascript MVC - separate logic

I currently use my own setup for an MVC framework in Javascript, but I am a bit confused about the fact, on where I should put my HTML events handlers.

Like click/mouse/focus event and stuff like that, is it acceptable to attach this event-logic into the controller.

Right now, I have it like this:

So, is it wisely to attach the click handlers for the view into the controller? or is that bad practise?

Upvotes: 4

Views: 734

Answers (3)

barry
barry

Reputation: 249

Okay thanks all!

What I am going todo:

/controllers/ -> some-module.js

/models/ -> some-module.js (holds data, keep track of states, retreive data)

/views/ -> some-module.js (attaching DOM events, DOM manipulations, will retreive the template TPL file and use it)

/templates/ -> some-module.tpl (the actual view in script-tags, similar to mustache or Jquery Templates, allows me to use variable-tags in the template)

This will keep my application organized and modular.

I could consider handling DOM events inside the controller, but I also need a location to execute some DOM manipulations (changing classnames, changing innerHTML values etc.), and I think that the controller is not the correct way to do that.

Upvotes: 0

Greg Franko
Greg Franko

Reputation: 1770

Putting event handlers inside of a controller class is not bad practice. In fact, the JavaScriptMVC library uses a Controller class to organize all event handlers (although this is not the sole purpose of the Controller class).

Putting event handlers inside of a View class is not bad practice either, as this is used by Backbone.js to organize all event handlers associated with a particular DOM element.

There are plenty of front-end MVC design patterns, and there is not one boilerplate that will fit perfectly for every situation.

Upvotes: 2

gdoron
gdoron

Reputation: 150253

Save the javascript and all the DOM manipulation in a js file.

Separation of concerns

BTW: Controller is sort of keyword in MVC, you might want to change th js file name, to a name without controller inside of it.

Upvotes: 0

Related Questions