Wednesday, July 12, 2006

MVC, as simple as HTML?

I recently got to thinking about MVC as it relates to GWT, AJAX, the Web, etc. Of course, there are many MVC(2) frameworks out there - Struts, Webworks, Spring MVC, etc. But, there's a simple MVC, I think.
  • Model :: XHTML
  • View :: CSS
  • Controller :: JavaScript
Let's think about this for a second. If XHTML is the model, it contains the state of "system" (the page in this case). If the XHTML is done correctly, it contains only structural/semantic markup, right? No presentation information. This is just the data, and that's the model, pretty simple.

Now, it makes sense that CSS is the view. It is the presentation of the data. As most people have seen with the CSS Zen Garden, the CSS can dramatically change the view/presentation of the data. CSS can also do minimal interaction (e.g. :hover). CSS is the view, that seems pretty simple, too.

Now the controller. JavaScript? Yep, I think so. If by JavaScript we take into account the browser itself, JavaScript is "observing" the view by monitoring mouse movements (onhover, onblur, etc.) and many other things as well. The controller then will modify the model (AJAX, modifying innerHtml, etc.). Changes in the model will automatically be reflected in the view (e.g. if JavaScript changes a node's class).

The more I think about it, the more I am satisfied that XHTML + CSS + JavaScript is indeed a simple example of MVC.

No comments: