Skip to content

Instantly share code, notes, and snippets.

@Caballerog Caballerog/blog.md Secret
Created Oct 9, 2019

Embed
What would you like to do?

Introduction

This post is the first in a series of three posts that will understand how the MVC architecture works to create frontend applications. The objective of this series of posts is to understand how to structure a frontend application by evolving a web page in which JavaScript is used as a scripting language towards an application in which JavaScript is used as an object-oriented language.

In this first post, the application will be built using VanillaJS. Therefore, this article is where the largest amount of code related to the DOM will be developed. However, it is very important to understand how all the parts of the application are related and how it is structured.

In the second article, we will reinforce the JavaScript code by transforming it into its TypeScript version.

Finally, in the last article we will transform our code to integrate it with the Angular framework.


Project Architecture

There is nothing more valuable than an image to understand what we are going to build, there is a GIF below in which the application we are going to build is illustrated.

demo

This application can be built using a single JavaScript file which modifies the DOM of the document and performs all operations, but this is a strongly coupled code and is not what we intend to apply in this post.

What is the MVC architecture? MVC is an architecture with 3 layers / parts:

  • Models - Manage the data of an application. The models will be anemic (they will lack functionalities) since they will be referred to the services.
  • Views - A visual representation of the models.
  • Controllers - Links between services and views.

Below, we show the file structure that we will have in our problem domain:

folders

The index.html file will act as a canvas on which the entire application will be dynamically built using the root element. In addition, this file will act as a loader of all the files since they will be linked in the html file itself.

Finally, our file architecture is composed of the following JavaScript files:

  • user.model.js - The attributes (the model) of a user.
  • user.controller.js - The one in charge of joining the service and the view.
  • user.service.js - Manage all operations on users.
  • user.views.js - Responsible for refreshing and changing the display screen.

The HTML file is the one shown below:

https://gist.github.com/fab74dbf80d8daf84323f45be5200958


Models (anemic)

The first class built in this example is the application model, user.model.js, which consists of the class attributes, and a private method that is generating random IDs (these id's could come from a database in the server).

The models will have the following fields:

  • id. Unique value.
  • name. The name of the users.
  • age. The age of the users.
  • complete. Boolean that lets you know whether we can cross the user off the list.

The user.model.js is shown below:

https://gist.github.com/9ebf410949926f5bb1e65553d09556ec


Services

The operations performed on users are carried out in the service. The service is what allows the models to be anemic, since all the logic load is in them. In this specific case, we will use an array to store all users and build the four methods associated with reading, modifying, creating and deleting (CRUD) users. You should note that the service makes use of the model, instantiating the objects that are extracted from LocalStorage to the User class. This is because LocalStorage only stores data and not prototypes of stored data. The same happens with the data that travels from the backend to the frontend, they do not have their classes instantiated.

The constructor of our class is as follows:

https://gist.github.com/d043d946617b3731a7d34123bd531902

Note that we have defined a class variable called users that stores all users once they have been transformed from a flat object to a prototyped object of the User class.

The next thing we must define in the service will be each of the operations we want to develop. These operations are shown below using ECMAScript, without using a single line in TypeScript:

https://gist.github.com/0cca0e95c0277abf58fe047ca5343f9a

It remains to be defined the commit method that is responsible for storing the operation performed in our data store (in our case LocalStorage).

https://gist.github.com/201c52c297e015566040b2ea2b6e57a7

This method invokes a callback function that has been binded when creating the Service, as it can be seen in the definition of the ```bindUserListChanged```` method. I can already tell you that this callback is the function that comes from the view and is responsible for refreshing the list of users on the screen.

The file user.service.js is as follows:

https://gist.github.com/81a5ead0479a376530010dbdf53d13ba


Views

The view is the visual representation of the model. Instead of creating HTML content and injecting it (as it is done in many frameworks) we have decided to dynamically create the whole view. The first thing that should be done is to cache all the variables of the view through the DOM methods as shown in the view constructor:

https://gist.github.com/db35f79439871b9275bcb6fd99284858

The next most relevant point of the view is the union of the view with the service methods (which will be sent through the controller). For example, the bindAddUser method receives a driver function as a parameter that is the one that will perform the addUser operation, described in the service. In the bindXXX methods, the EventListener of each of the view controls are being defined. Note that from the view we have access to all the data provided by the user from the screen; which are connected through the handler functions.

https://gist.github.com/b55c0e4e6c14f436faa17a02b60bdc1f

The rest of the code of the view goes through handling the DOM of the document. The file `` user.view.js``` is as follows:

https://gist.github.com/28689a9b5ff2a110a4f138d036d2c6fc


Controllers

The last file of this architecture is the controller. The controller receives the two dependencies it has (service and view) by dependency injection (DI). Those dependencies are stored in the controller in private variables. In addition, the constructor makes the explicit connection between view and services since the controller is the only element that has access to both parties.

The file user.controller.js is the one shown below:

https://gist.github.com/5c7bcb341a622dd4cf78413bd3f41512


App.js

The last point of our application is the application launcher. In our case, we have called it app.js. The application is executed through the creation of the different elements: UserService, UserView and UserController, as shown in the file ```app .js``.

https://gist.github.com/1ca9de119d7d96fcba4275deb50f1fbc


Conclusions

In this first post, we have developed a Web application in which the project has been structured following the MVC architecture in which anemic models are used and the responsibility for the logic lies on the services.

It is very important to highlight that the didactical of this post is to understand the structuring of the project in different files with different responsibilities and how the view is totally independent of the model/service and the controller.

In the following article, we will reinforce JavaScript using TypeScript, which will give us a more powerful language to develop Web applications. The fact that we have used JavaScript has caused us to write a lot of verbose and repetitive code for the management of the DOM (this will be minimized using the Angular framework).


The GitHub branch of this post is https://github.com/Caballerog/VanillaJS-MVC-Users

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.