Challenge: Solution Review
Explore the MVC architectural pattern by examining how a JavaScript shopping cart app uses model, view, and controller components. Understand how user actions update the model via the controller and how the view reflects these changes, reinforcing a clear understanding of component roles and interactions within MVC.
We'll cover the following...
Solution #
Explanation
The program simply displays information about the items in the shopping cart. As discussed, it is divided into three components:
-
The shopping cart object is represented by the model
ShoppingCartModel -
The information about the items in the cart is displayed by the view component
ShoppingCartView -
The model and view components are connected through the controller
ShoppingCartController
Let’s take a look at each component one by one.
ShoppingCartModel
class ShoppingCartModel
{
constructor(itemNumber,itemName, itemQuantity, itemPrice){
this.itemName = itemName;
this.itemQuantity = itemQuantity;
this.itemPrice = itemPrice;
this.itemNumber = itemNumber
}
getItemName(){
return this.itemName;
}
getItemQuantity(){
return this.itemQuantity
}
getItemPrice(){
return this.itemPrice;
}
getItemNumber(){
return this.itemNumber;
}
}
The ShoppingCartModel presents a shopping cart object which has the ...