Just a View: An Introduction To Model-View-Controller Pattern

36
Aaron Nordyke, Sr. Software Engineer It’s Just a View model view controller An Introduction to

description

Presentation I gave to my team as an introduction to MVC.

Transcript of Just a View: An Introduction To Model-View-Controller Pattern

Page 1: Just a View:  An Introduction To Model-View-Controller Pattern

Aaron Nordyke, Sr. Software Engineer

It’s Just a View

model view controller

An Introduction to

Page 2: Just a View:  An Introduction To Model-View-Controller Pattern

Separation of Concerns

Page 3: Just a View:  An Introduction To Model-View-Controller Pattern

g

UGLY e

Spaghetti Code

h t

Page 4: Just a View:  An Introduction To Model-View-Controller Pattern

There once was

a drug named

Xigris…

Page 5: Just a View:  An Introduction To Model-View-Controller Pattern

Xigris

Xigris

Xigris

Xigris

Xigris

Xigris

Xigris

Xigris

Xigris

Xigris

Xigris Xigris

Xigris

Page 6: Just a View:  An Introduction To Model-View-Controller Pattern

Modules

GOOD e h t

Page 7: Just a View:  An Introduction To Model-View-Controller Pattern

Engineers Made a bet

Two

Page 8: Just a View:  An Introduction To Model-View-Controller Pattern
Page 9: Just a View:  An Introduction To Model-View-Controller Pattern

Developer Sanity Maximize

Page 10: Just a View:  An Introduction To Model-View-Controller Pattern

g

MVC

BADASS e h t

Page 11: Just a View:  An Introduction To Model-View-Controller Pattern

DATA DOM

Page 12: Just a View:  An Introduction To Model-View-Controller Pattern

Controller

Model View

Data

Business Logic

DB Interaction

User Interaction

HTML

View to Model

Interaction

One Direction

Observer

Pattern

Page 13: Just a View:  An Introduction To Model-View-Controller Pattern

Controller

Model View

User interacts

with a view

1

View alerts

controller of

particular

event

2

Controller Updates Model 3

Model alerts view that

it has changed.

4

View grabs model data

and updates itself.

5

Page 14: Just a View:  An Introduction To Model-View-Controller Pattern

Controller

Model View 1

User interacts

with a view 1

View alerts

controller of

particular

event

2

Controller Updates Model 3

Model alerts

view that it has

changed.

4

View grabs model data

and updates itself.

5 View 2

Page 15: Just a View:  An Introduction To Model-View-Controller Pattern

Controller

Model View

Data

Business Logic

DB Interaction

User Interaction

HTML

View to Model

Interaction

One Direction

Observer

Pattern

Page 16: Just a View:  An Introduction To Model-View-Controller Pattern

Models

Controllers

Views

Percentage of Code

Page 17: Just a View:  An Introduction To Model-View-Controller Pattern

View

The page is not the view

Page 18: Just a View:  An Introduction To Model-View-Controller Pattern

View

View

View

View

View

View

View

View

View

View

View

View

View

View

View

The page contains the views

Page 19: Just a View:  An Introduction To Model-View-Controller Pattern

MVC’s Bestest Friends

Observer Templating Pattern Library

MVC Framework

Page 20: Just a View:  An Introduction To Model-View-Controller Pattern

Observer Pattern

Best Friend #1

Page 21: Just a View:  An Introduction To Model-View-Controller Pattern

Controller

Model View

Data

Business Logic

DB Interaction

User Interaction

HTML

View-Model

Interaction

One Direction

Observer

Pattern

Page 22: Just a View:  An Introduction To Model-View-Controller Pattern

Mother Babysitter

“If little Billy gets hurt,

I want you to call this

number immediately.”

Little Billy breaks his

arm while ice-skating,

so babysitter calls the

supplied number.

1

2

Lame Real-world Analogy

Page 23: Just a View:  An Introduction To Model-View-Controller Pattern

Observer Subject

“If this thing I care

about happens,

call this function.”

The things happens,

so the subject calls

the function.

1

2

Observer and Subject

Page 24: Just a View:  An Introduction To Model-View-Controller Pattern

Observer

Subject

Observer Observer Observer

Observer

Observer

Observer

Observer

Page 25: Just a View:  An Introduction To Model-View-Controller Pattern

View

(Observer)

Model

(Subject)

“If this certain data

changes, call my

function view.foo().

The change happens,

so the model calls

view.foo().

1

2 3 The view grabs the

changed data from the

model and updates

itself.

View-Model Relationship

Page 26: Just a View:  An Introduction To Model-View-Controller Pattern

view.prototype.render = function(){ //grab model data and update view html } /* * view tells model that if “change” happens, * then call view’s render function */ model.subscribe(“change”,view.render); /* * The “change” happens, so the model alerts * any observers of “change” */ model.notify(“change”);

1

2

3

Observer Pattern – Basic Use

Page 27: Just a View:  An Introduction To Model-View-Controller Pattern

var events = [ {“abitraryString1” : [function1, function2] }, //model.notify(“arbitraryString2”) would //cause function2 and function3 to be called. {“abritraryString2” : [function2, function3] }, //model.subscribe(“arbitraryString3”,function4) //would add function4 to this list {“abritraryString3” : [function3] }, //model.subscribe(“arbitraryString4”,function1) //would add a new member to the events array ];

Observer Pattern -- Internals

Page 28: Just a View:  An Introduction To Model-View-Controller Pattern

{{Templating}} Library

Best Friend #2

Page 29: Just a View:  An Introduction To Model-View-Controller Pattern

var container = Util.cep("div",{ "className":"wrap", }); var firstName = Util.cep("span",{ "className":"name", "innerHTML":person.firstName }); var lastName = Util.cep("span",{ "className":"name", "innerHTML":person.lastName }); Util.ac(firstName,container); Util.ac(lastName,container);

Look Familiar?

Page 30: Just a View:  An Introduction To Model-View-Controller Pattern

//template <div class="wrap"> <span class="name">{{firstName}}</span> <span class="name">{{lastName}}</span> </div>

var html = Mustache.to_html(template,person)

Replaced with {{Templates}}

Page 31: Just a View:  An Introduction To Model-View-Controller Pattern

Popular Templates

mustache.js

http://mustache.github.com/

Minimal Templating on Steroids

http://www.handlebarsjs.com/

Logic-less templates

}

Page 32: Just a View:  An Introduction To Model-View-Controller Pattern

Best Friend #3

Framework

Page 33: Just a View:  An Introduction To Model-View-Controller Pattern

Sole focus Help you do MVC

Page 34: Just a View:  An Introduction To Model-View-Controller Pattern

• Classes for the separate concerns of

Models, Views, and Controllers

• Observer Pattern built-in

• Templating built-in

• Event Delegation built-in

• Small -- 4.6kb, compressed

Page 35: Just a View:  An Introduction To Model-View-Controller Pattern

“It's all too easy to create

JavaScript applications that end

up as tangled piles of jQuery

selectors and callbacks, all trying

frantically to keep data in sync

between the HTML UI, your

JavaScript logic, and the database

on your server.” Jeremy Ashkenas, Creator of Backbone.js

Page 36: Just a View:  An Introduction To Model-View-Controller Pattern

SUMMARY

Model-View-Controller

Separation of concerns

Friends of MVC