Step 2: Write the Model Code

Let’s take a look at how to set up the model class file for the web application.

In this step, we’ll write the code of our model class and save it in a specific model class file. In an MVC application, the model code is the most important part of the application. It’s also the basis for writing the view and controller code.

In fact, large parts of the view and controller code can be automatically generated from the model code. Many MVC frameworks provide this kind of code generation.

In the information design model shown in the previous lesson, there’s only one class, representing the object type Book. This figure can be found below as well:

In the folder src/m, we’ll create a file Book.js that initially contains the following code:

Press + to interact
function Book( slots) {
this.isbn = slots.isbn;
this.title = slots.title;
this.year = slots.year;
};

The model class Book is coded as a JavaScript constructor function with a single slots parameter—a record object with fields isbn, title, and year—representing the constructor parameters to be assigned to the ISBN. The ...