design patterns - How do I separate DOM view logic from DOM event (controller?) logic in JavaScript? -


I'm not sure that I'm distinguishing my application correctly in MVC logic. I'm confused about how to deal with the DOM, which is related to both visual and event logic. For example:

  var HistoryView = function ($ siblingEl) {this. $ El = $ ('
gt;' + '& lt; button class = "backward" & gt; & lt; & lt; / button & gt;' + '& lt; Button class = "forward"> gt; & gt; & lt; / button & gt; '+' & lt; / div & gt; '); $ SiblingEl.after (this. $ El); $ ('Behind') ('Click', function () {events.fire ('clickPrev');}); ...

and then a controller:

  var HistoryManager = function (board, game) {events.on ('clickPrev', function) { Var move = History.getPrev (); If (move) {board.undoMove (move); Game.turn - = 1; History.undo ();}}); ...  

A good part of the code in my view is dedicated to listening to the events of DOM and then send them to my custom event metaobject; My controller listens to event instead of DOM. It seems that the controller has listened directly to Dom, but then it should be aware of watching it. Is there a way to deal with this? In the spinal cord, it seems that the scene just gets smarter (a scene becomes the controller) What are some other ways?

To break a good comparison between different methods of implementing an MV * pattern break MVC actually Useful I use Google to shut down, and my priority is to merge the controller and see it in one. However, with Closer you can separate them, create visuals, implement an interface for HTML and visible objects that may require a controller. See goog.ui.Control and goog.ui.ControlRenderer for an example. In your case, methods like getBackButton () and getForwardButton () will be provided in this view. The controller will then listen to the DOM event instead of the custom view event.

However, other people think that one view should listen to the events of the DOM and a controller should listen to the events (according to your example). I do not think there is a right way to do things. The most important thing is that you can easily (unit) test your code;)


Comments