Challenge: Solution Review

This lesson will explain the solution to the problem from the previous coding challenge.

We'll cover the following...

Solution #

Console

Explanation

This challenge required you to use the MVVM pattern to write a program that changes the color of the text to “green”, “red”, or “blue” when written in the input field.

It is divided into three components: the model, ViewModel, and view. Let’s take a look at them one by one.

Model

class Model{
	constructor(){
		this.model  = {color: "red"};
		this.observers = [];
	}
     //code...

The Model constructor consists of the following properties:

  • model: The object containing the color. It is set to red by default.

  • observers: The list containing all the observers of the model.

Since the Model is a subject, it also contains the subscribe function to register an observer.

subscribe(observer){
		this.observers.push(observer);
}

It contains the notifyObservers function too, which ...