Beginning AngularJS

(WallPaper) #1
Chapter 3 ■ IntroduCtIon to MVC

Design patterns are indeed a valuable resource, but you still have to put plenty of thought into how (and whether
or not) to use any given design pattern in a particular context. As specific as design patterns may seem in their
description and usage, they are still generalized solutions that may or may not apply to your needs. That being said,
a well-documented design pattern will help you make these decisions.


■ Tip reading up on design patterns is actually a great way to improve your code. You may have solved a problem in


a particular way, only to find that there is a design pattern dedicated to avoiding the approach you took! It might not be


good for the ego, but it’s a great way to learn.


With this short introduction to design patterns now complete, we can look at the specific pattern that we will use
throughout the rest of this book: the Model View Controller (MVC) pattern. You have to learn what MVC is, so that you
can apply it within AngularJS. However, we don’t actually have to write our own MVC solution, because AngularJS has
what you need baked right into the framework.


Model View Controller

I hope our quick discussion about design patterns has brought to the front of your mind that there are good ways and
not so good ways to design your applications and that there are helpful recipes out there that can help you design
better applications. Fortunately, the folks who created AngularJS have already put all of the pieces of the MVC pattern
into place for you. As the MVC pattern is an architectural pattern, which is realized through a number of other design
patterns, I won’t include the rather extensive documentation for it here. Instead, we will focus on the AngularJS
implementation of it and consider what it does for us.
Let’s talk about the three major parts of the MVC pattern: the model, the view, and the controller. We’re not
really speaking at a code level here; rather, we are talking at a higher level about how to organize and structure your
applications. MVC is often considered an architectural pattern, which is essentially a pattern that addresses some
aspect of an application’s overall organization and structure.
We will see how MVC comes together in code form later in this chapter, so don’t worry too much if it all seems a
little abstract as I am discussing it.


■ Tip architectural patterns are often realized through a number of design patterns. as I said earlier, however, the


keyword here is pattern. It really depends on what level you happen to be speaking (and quite possibly to whom you


happen to be talking).


Model

The model represents the underlying, logical structure of data in a software application. It’s a common mistake to
think of the model as the database behind your application, and it is much better to view the model as the body of
code that represents the data.


View

A view is the body of code that represents the user interface (all of the things that the user can see and to which the
user can respond on the screen, such as buttons, dialog boxes, and so on). An application generally has multiple
views, and each view often represents some portion of your model.

Free download pdf