How I Adopted the Model, View, Controller (MVC) Architectural Pattern in JavaScript

Aaron Rory
5 min readMay 15, 2020

--

…for Better Code Separation

What is this Model, View, Controller (MVC) architectural pattern?

Source: Rails Documentation

The MVC architecture divides your code into three (3) layers Models, Views and Controllers separating the different responsibilities of the program.

MVC diagram from Wikipedia
Image from Wikipedia

Model Layer

This layer in Ruby on Rails contains the domain model, which generally represents a specific object class (e.g. a Person, Animal, Books). This is where the business logic is usually handled since this model is linked to the database and its data is derived from the relative table’s rows.

View Layer

Handles the visual representations of the responses provided by the controllers. Since the controller can return information as HTML, XML, JSON, etc.

Controller Layer

In Rails this layer is responsible for interacting with the model, manipulating its data and providing suitable responses to various HTTP requests.

Now, what would this MVC pattern look like in JavaScript?

Source: MDN web docs

Since JavaScript generally does not involve the use of databases (although it could), nor HTTP request handling (Once again it could), the MVC pattern will have to be tweaked a bit to fit the language specificity.

MVC diagram from MDN web docs
Image from MDN

Model Layer

The model layer can literally be as simple as an array but usually it would be a class. An application can have multiple models and these classes (models) would contain the basic data needed for the app to function.

E.G. Take for instance a Classroom App which tracks which classes a person is taking. The models in this case can be divided into classes such as a Classroom, Person and an Array based model called Subjects.

Base Model Classes

Classroom model

The Classroom model contains data variables which will hold information for each classroom. This would be a list of all the persons currently enrolled in this classroom, the subject associated with this classroom and it’s ID.

The Classroom model contains data variables which will hold information for each classroom. This would be a list of all the persons currently enrolled in this classroom, the subject associated with this classroom and it’s ID.

Person model

The Person model contains data variables which will hold information for each person. This would be their first and last name, the subjects that they are studying and which classrooms they are apart of.

Subjects model

The Subjects model will simply be an array, since for this example I have no intention in allowing the subjects model to be manipulated.

Controller Layer

The controller would be a class which translates the user input into changes to the model’s data.

E.G. In the Classroom App — the controller receives user input from view elements such as text input or select options and button clicks which is used to modify the model.

ClassroomController class manipulates the classroom model’s data

The classroom controller in this case could be seen as a Table in reference to how Rails operates and each row in this “Table” would be information linked to each classroom object already created.

This controller has three variables of its own, “lastID (Every time a classroom object is created and added to the classrooms array this variable’s value increments), “classrooms (an array of all created classroom objects) and “selectedClass”.

View Layer

This layer handles the visual representation of the app’s data. This layer contains classes which allows the User to see and interact with the data.

E.G. In the Classroom App — the view would provide Document Object Model (DOM) elements such as Buttons, Inputs and containers (<div/>, <span/ >, <p/>…etc.) to display the various persons and classrooms and their related data.

Classroom App Browser Display
Image of Both ClassroomView and PersonView for Classroom App
ClassroomView used to show classroom model data and translate user interactions to the controller

The ClassroomView class contains a variable that links to a ClassroomController which is created upon construction. This allows communication with the controller from the view.

The updateView() function is ran after every change as a result from user Interactions. This function simply updates all related view DOM elements with the appropriate data obtained from the associated model.

All functions within the view simply grab values from the User Interface (UI) DOM elements and transfer them as variables to the controller’s functions. The functions selectClassroom(), addClassroom() and removeClassroom() are all added to the DOM elements via the updateView() function as events through the addEventListener() function.

Accessing all controllers and views through a single view

Now, since for this example we have two controllers, a Classroom Controller and a Person Controller (Shown in full project). We would also have two views and if we wanted these two views to be able to interact with one another we would create a single overarching view. We could call this view the Application View.

ApplicationView class created to combine all views under a single entity

This ApplicationView class would have it’s own variables that would link to both the classroom and person views. Since it has access to those two views it also has access to their controllers.

Image of Add selected Person to Selected Class button
Button added to Browser Display via the Application View

This button above is generated by the Application view. It grabs the selectedClassroom and selectedPerson values from their respective controllers and runs the addPerson() function in the classroom view upon interaction.

Some Advantages of Using the MVC Framework

Sources: Brainvire, c-sharpcorner, StackOverflow, Wikipedia

1. Separation of Concerns

All code related to the User Interface is handled by the view. All base data variables are contained by the Model and all model data is changed through the Controller.

2. Simultaneous Development

Since this MVC model clearly separates the project into three (3) layers it becomes a lot easier to separate and assign tasks to multiple developers.

3. Ease of Modification

Can easily make changes to each layer without immediately affecting the other layers.

4. Test Driven Development (TDD)

With clear separation of concerns it allows the testing of each individual component independently.

--

--

Aaron Rory

Full Stack Web Developer — Studying JavaScript, HTML, CSS, Node.js, SQL, Ruby and Rails as well as C# and Unity. @ARN GithubProfile: https://github.com/Aaron-RN