The Model View Controller Pattern - MVC Architecture and Frameworks Explained
The Model View Controller Pattern - MVC Architecture and Frameworks Explained
What is MVC?
MVC stands for model-view-controller. Here's what each of those
components mean:
The MVC pattern helps you break up the frontend and backend
code into separate components. This way, it's much easier to
manage and make changes to either side without them
interfering with each other.
Now let's dive into these three components that make up the
MVC architecture pattern.
Model (data)
The model's job is to simply manage the data. Whether the data is
from a database, API, or a JSON object, the model is responsible
for managing it.
Search 10,900+ tutorials Forum Donate
In the Car Clicker application, the model object contains an array
Learn to code — free 3,000-hour curriculum
of car objects with all the information (data) needed for the app.
const model = {
currentCar: null,
cars: [
{
clickCount: 0,
name: 'Coupe Maserati',
imgSrc: 'img/black-convertible-coupe.jpg',
},
{
clickCount: 0,
name: 'Camaro SS 1LE',
imgSrc: 'img/chevrolet-camaro.jpg',
},
{
clickCount: 0,
name: 'Dodger Charger 1970',
imgSrc: 'img/dodge-charger.jpg',
},
{
clickCount: 0,
name: 'Ford Mustang 1966',
imgSrc: 'img/ford-mustang.jpg',
Search 10,900+ tutorials Forum Donate
},
{
Learn to code — free 3,000-hour curriculum
clickCount: 0,
name: '190 SL Roadster 1962',
imgSrc: 'img/mercedes-benz.jpg',
},
],
};
Views (UI)
The view's job is to decide what the user will see on their screen,
and how.
The Car Clicker app has two views: carListView and CarView .
Both views have two critical functions that define what each view
wants to initialize and render.
These functions are where the app decides what the user will see
and how.
carListView
Search 10,900+ tutorials Forum Donate
const carListView = {
init() {
Learn to code — free 3,000-hour curriculum
// store the DOM element for easy access later
this.carListElem = document.getElementById('car-list');
// render this view (update the DOM elements with the right values)
this.render();
},
render() {
let car;
let elem;
let i;
// get the cars to be render from the controller
const cars = controller.getCars(); ADVERTISEMENT
CarView
const carView = {
init() {
// store pointers to the DOM elements for easy access later
this.carElem = document.getElementById('car');
this.carNameElem = document.getElementById('car-name');
this.carImageElem = document.getElementById('car-img');
this.countElem = document.getElementById('car-count');
this.elCount = document.getElementById('elCount');
// render this view (update the DOM elements with the right values)
this.render();
},
Search 10,900+ tutorials Forum Donate
handleClick() {
Learn to code — free 3,000-hour curriculum
return controller.incrementCounter();
},
render() {
// update the DOM elements with values from the current car
const currentCar = controller.getCurrentCar();
this.countElem.textContent = currentCar.clickCount;
this.carNameElem.textContent = currentCar.name;
this.carImageElem.src = currentCar.imgSrc;
this.carImageElem.style.cursor = 'pointer';
},
};
Controller (Brain)
The controller's responsibility is to pull, modify, and provide data
to the user. Essentially, the controller is the link between the view
and model.
If there
Searchare any updates
10,900+ tutorials from the views, it modifies the data with Forum Donate
a setter function. Learn to code — free 3,000-hour curriculum
const controller = {
init() {
// set the current car to the first one in the list
model.currentCar = model.cars[0];
getCurrentCar() {
return model.currentCar;
},
getCars() {
return model.cars;
},
MVC Frameworks
JavaScript has grown in popularity, and it's taken over the
backend in recent years. More and more full-blown JavaScript
applications have opted for the MVC architecture pattern in one
way or another.
Frameworks come and go, but what has been constant are the ADVERTISEMENT
Conclusion
TheSearch
most10,900+
attractive concept of the MVC pattern is separation of
tutorials Forum Donate
concerns. Learn to code — free 3,000-hour curriculum
**If you want to take a look at the Car Clicker app, the code is
available on GitHub or checkout the live version here.**
Rafael D. Hernandez
Web Developer | Global Language Translations Lead at @freeCodeCamp
If you read
Search this far,
10,900+ thank the author to show them you care.
tutorials Say Thanks Forum Donate
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people
get jobs as developers. Get started
ADVERTISEMENT
Mobile App
Our Charity
About Alumni Network Open Source Shop Support Sponsors Academic Honesty Code of Conduct Privacy Policy Terms of Service
Copyright Policy