Web development has changed significantly in the past few years; it hasn’t been long since deploying a web project simply involved uploading static HTML, CSS and JavaScript files to a HTTP server. The growing popularity with providing software as a service has meant that applications which have resided on the desktop are being transferred to the browser.
Some of these web applications are large scale and complex, JavaScript alone cannot be used to provide a stable foundation to write quality, maintainable code. As a result, new MVC frameworks have appeared that offer to provide structure and guidance when developing these applications.
What are they?
MVC frameworks are libraries that can be included alongside JavaScript to provide a layer of abstraction on top of the core language. Their goal is to help structure the code-base and separate the concerns of an application into three parts:
- Model - Represents the data of the application. This matches up with the type of data a web application is dealing with, such as a user, video, picture or comment. Changes made to the model notify any subscribed parties within the application.
- View - The user interface of the application. Most frameworks treat views as a thin adapter that sits just on top of the DOM. The view observes a model and updates itself should it change in any way.
- Controller - Used to handle any form of input such as clicks or browser events. It’s the controller’s job to update the model when necessary (i.e. if a user changes their name).
Not all frameworks follow the MVC pattern. You may see some frameworks utilize a variation of the MVC pattern such as MVVM or MVP.
If you’re unfamiliar with the MVC pattern or the variations used by some frameworks a good idea is to read JavaScript Design Patterns to help your understanding.
Why are they needed?
A DOM manipulation library such as jQuery coupled with utility libraries (underscore, modernizr) can make building webpages much easier. However, these libraries lose their usefulness when used to build web applications.
Web applications are unlike a normal web page, they tend to feature more user interaction as well as needing to communicate with a backend server in real time. If you were to handle this behaviour without an MVC framework you’d end up writing messy, unstructured, unmaintainable and untestable code.
When should you use them?
You should consider utilizing an MVC framework if you’re building an application with enough heavy-lifting on the client-side to struggle with JavaScript alone. Choose incorrectly and you’ll end up re-inventing the functionality provided by an MVC framework.
Be aware, if you’re just building an application that still has a lot of the heavy lifting on the server-side (i.e. view generation) and there is little interaction on the client-side, you’ll find using an MVC framework is likely overkill. In that case, it’s better to use a simpler setup such as a DOM manipulation library with a few utility add-ons.
The following checklist is not exhaustive but hopefully provides enough context to help decide whether an MVC framework is suitable for what you’re building:
- Your application needs an asynchronous connection to the backend
- Your application has functionality that shouldn’t result in a full page reload (i.e. adding a comment to a post, infinite scrolling)
- Much of the viewing or manipulation of data will be within the browser rather than on the server
- The same data is being rendered in different ways on the page
- Your application has many trivial interactions that modify data (buttons, switches)
Good examples of web applications that fulfil these criteria are Google Docs, Gmail or Spotify.
0 comments:
Post a Comment