SlideShare a Scribd company logo
Structuring a Client-Side App
6.170 Software Studio
Fall 2015
Eirik Bakke
Objectives
• After today’s class, you should understand what a
model and a view is, and how to separate the
two.
• You should understand the subscriber pattern.
• We will also see events, DOM building, unit
testing, and the object construction idiom in
action.
• Today’s complete app example is relevant to both
the Game of Life and the Fritter assignments
Today’s Example (don’t look at the source
yet—spoilers! :-)
http://shoutkey.com/even
How would you structure this app?
Common User Interface Architectures
Spaghetti Code
• Mixes data and
presentation
• Everything depends
on everything
• State is everywhere
• Repeats logic
• Small features
require changes
throughout codebase
Spaghetti Code
Model/View/Controller (MVC)
Model/ViewController
(from Java’s Swing toolkit)
View/ViewModel/Model
(e.g. the Knockout framework)
The Point Is
View
(Presentation/
User Interface)
Model
(Underlying
Data)
• Keep the model and the view separate
• Your model should not depend on your view
• You should be able to write and test your model
without writing a line of HTML or CSS (or other
user interface code).
Back to Our Example
gradewidget.js
(view)
gradebook.js
(model)
• Our app consists of two separate modules
• The model does not depend on the view
• We will write and rigorously test our model,
gradebook.js, before doing any work on the
view (gradewidget.js)
Back to Our Example
gradewidget.js
(view)
gradebook.js
(model)
Back to Our Example
grademain.js tests.js
depends-on
gradewidget.js
gradewidget.css
(view)
gradebook.js
(model)
Back to Our Example
index.html
grademain.js
tests.html
tests.js
depends-on
Implementation Steps
1. Basic model class
2. Robust model class, unit tests
3. Documented model class
4. Read-only grade widget—the view
5. Editable grade widget
6. Two widgets, one model (buggy)
7. Subscriber pattern
8. Reconfigure one of the widgets
shoutkey.com/a
sparagus
Basic Model Class shoutkey.com/a
sparagus
Robust Model Class, Unit Tests
shoutkey.com/a
sparagus
Robust Model Class, Unit Tests
shoutkey.com/a
sparagus
Robust Model Class, Unit Tests
shoutkey.com/a
sparagus
Documented Model Class shoutkey.com/a
sparagus
Read-Only Grade Widget (the “view”)
Read-Only Grade Widget (the “view”)
Read-Only Grade Widget (the “view”)
Read-Only Grade Widget (the “view”)
Editable
Grade
Widget
Editable
Grade
Widget
Editable
Grade
Widget
new grade set on the model,
not on the view!
Two Widgets, One Model (buggy)
What’s the bug?
Challenge: Fix the Bug tinyurl.com/
pq9gm8v
Subscriber Pattern
gradewidget.js
(view)
gradebook.js
(model)
Subscriber Pattern
• The subscriber pattern is used to notify the view of
changes in the model, without requiring the model to
depend on the view
• Subscribers are analogous to DOM event handlers;
except the events are on changes to your model, and
your model class is responsible for managing and
calling subscribers.
• Also known as “observer” pattern or “publish-
subscribe” pattern
Fixing the bug: Subscriber Pattern
gradebook.js
Fixing the bug: Subscriber Pattern
gradewidget.js
Reconfigure One of the Widgets
Reconfigure One of the Widgets
Reconfigure One of the Widgets
Conclusion
• Keep your app’s data (the “model”) separate
from its user interface (the “view”)
• The model should not depend on the view
• Write and test the model, with unit tests and
runtime checks, before writing any user
interface code
• The subscriber pattern can be used to notify
the view of changes in the model, without
requiring the model to depend on the view
• When the user edits data, apply the change to
the model, not the view

More Related Content

What's hot (20)

PPTX
Model View Presenter (MVP) In Aspnet
rainynovember12
 
PPTX
Test driving an MVVM App
Emanuele DelBono
 
PPTX
Introduction To Model View Presenter
saeed shargi ghazani
 
PPTX
Get satrted angular js
Alexandre Marreiros
 
PPTX
Angularjs 5,6,7
ankitarasal
 
PPTX
Joomla Frameworks Kung Fu
Oleg Nesterov
 
PDF
Ui design patterns
Jorge Ortiz
 
PPT
Joomla Extensions Kung Fu
Oleg Nesterov
 
PPTX
Mvvm in the real world tccc10
Bryan Anderson
 
PPTX
How do you get your idea into the app store?
Gabe Kwakyi
 
PPT
Savsoft Test v6.0
savsoft
 
PDF
Web Development using ASP.NET MVC at HEC
Adil Mughal
 
PDF
Mvp pattern
Khuong Vo
 
PPTX
AngularJS && Angular
Hoa Le
 
PPTX
Angularjs overview
VickyCmd
 
PPTX
deliver:Agile 2019 "Testing Microservices: From Development to Production
Daniel Bryant
 
PDF
Introduction to Angular Js
Professional Guru
 
PPTX
Web Development with Visual Studio 2012
Raffaele Fanizzi
 
PDF
How I Accidentally Discovered MVVM
Bradford Dillon
 
PPT
Factory Method Design Pattern
melbournepatterns
 
Model View Presenter (MVP) In Aspnet
rainynovember12
 
Test driving an MVVM App
Emanuele DelBono
 
Introduction To Model View Presenter
saeed shargi ghazani
 
Get satrted angular js
Alexandre Marreiros
 
Angularjs 5,6,7
ankitarasal
 
Joomla Frameworks Kung Fu
Oleg Nesterov
 
Ui design patterns
Jorge Ortiz
 
Joomla Extensions Kung Fu
Oleg Nesterov
 
Mvvm in the real world tccc10
Bryan Anderson
 
How do you get your idea into the app store?
Gabe Kwakyi
 
Savsoft Test v6.0
savsoft
 
Web Development using ASP.NET MVC at HEC
Adil Mughal
 
Mvp pattern
Khuong Vo
 
AngularJS && Angular
Hoa Le
 
Angularjs overview
VickyCmd
 
deliver:Agile 2019 "Testing Microservices: From Development to Production
Daniel Bryant
 
Introduction to Angular Js
Professional Guru
 
Web Development with Visual Studio 2012
Raffaele Fanizzi
 
How I Accidentally Discovered MVVM
Bradford Dillon
 
Factory Method Design Pattern
melbournepatterns
 

Similar to Structuring a Client-Side App (20)

PPTX
iOS Coding Best Practices
Jean-Luc David
 
PDF
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
 
PDF
Just a View: An Introduction To Model-View-Controller Pattern
Aaron Nordyke
 
PDF
Task 2 - Educational Article – Model View Controller (MVC)
Shubham Goenka
 
PPT
Design patterns
mudabbirwarsi
 
PDF
Session 7 - Overview of the iOS7 app development architecture
Vu Tran Lam
 
KEY
iOS Design Patterns
Andreas Blick
 
PPT
JAVA design patterns and Basic OOp concepts
Rahul Malhotra
 
PPT
Building Rich User Experiences Without JavaScript Spaghetti
Jared Faris
 
KEY
AngularJS for designers and developers
Kai Koenig
 
PDF
Design patterns in javascript
Ayush Sharma
 
PDF
CS2810Team6GroupReport
Dan Coles
 
PPTX
Observer Design Pattern to Observe Something
hamzanaeemllc
 
PPT
Design Pattern with Actionscript
Daniel Swid
 
PDF
Writing clean code
Angel Garcia Olloqui
 
PDF
Javascript spaghetti stirtrek_5_17
Jared Faris
 
PPTX
jquery summit presentation for large scale javascript applications
DivyanshGupta922023
 
PDF
design pattern presentation 2.pdf
JonathasOliveira50
 
PDF
Modern UI Architecture_ Trends and Technologies in Web Development
Suresh Patidar
 
PPTX
L03 Design Patterns
Ólafur Andri Ragnarsson
 
iOS Coding Best Practices
Jean-Luc David
 
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
 
Just a View: An Introduction To Model-View-Controller Pattern
Aaron Nordyke
 
Task 2 - Educational Article – Model View Controller (MVC)
Shubham Goenka
 
Design patterns
mudabbirwarsi
 
Session 7 - Overview of the iOS7 app development architecture
Vu Tran Lam
 
iOS Design Patterns
Andreas Blick
 
JAVA design patterns and Basic OOp concepts
Rahul Malhotra
 
Building Rich User Experiences Without JavaScript Spaghetti
Jared Faris
 
AngularJS for designers and developers
Kai Koenig
 
Design patterns in javascript
Ayush Sharma
 
CS2810Team6GroupReport
Dan Coles
 
Observer Design Pattern to Observe Something
hamzanaeemllc
 
Design Pattern with Actionscript
Daniel Swid
 
Writing clean code
Angel Garcia Olloqui
 
Javascript spaghetti stirtrek_5_17
Jared Faris
 
jquery summit presentation for large scale javascript applications
DivyanshGupta922023
 
design pattern presentation 2.pdf
JonathasOliveira50
 
Modern UI Architecture_ Trends and Technologies in Web Development
Suresh Patidar
 
L03 Design Patterns
Ólafur Andri Ragnarsson
 
Ad

Recently uploaded (20)

PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PPTX
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Ad

Structuring a Client-Side App

Editor's Notes

  • #2: We’ve called this lecture “Structuring a Client-Side App” not because the lessons here only apply to the client-side portion of an app, but because we’ll be going through an example that’s entirely client-side.
  • #4: Show what the app does. Point out that this is all client-side. http://www.mit.edu/~ebakke/misc/6170_gradebook_example http://www.mit.edu/~ebakke/misc/6170_gradebook_example/08_pivot/index.html
  • #5: Give the class 5 minutes to think about how they would structure this app, then discuss possible architectures on the board. http://www.mit.edu/~ebakke/misc/6170_gradebook_example
  • #8: Spaghetti code originally refered to programs with tangled control structures (e.g. using GOTO everywhere). I’ll use it to denote software with tangled dependencies. This list is not exhaustive.
  • #9: This is just the classic MVC.
  • #10: This will not be on the test. I’m just showing you a couple of architectures people have proposed.
  • #11: This one adds another layer. The thing called “View” here is actually the DOM in this model.
  • #12: Explain clearly what is meant by “view” and “model”. The model is the data underlying your app. The view is the user interface. The view may have some state of its own (e.g. the pressed state of buttons, or the immediate content of text boxes), but the core state of your application, which may potentially be shared among multiple user interface components or even users, should be stored in a separate “model” part of your application that is independently testable and does not depend on anything else.
  • #13: Give the class 5 minutes to think about how they would structure this app, then discuss possible architectures on the board. http://www.mit.edu/~ebakke/misc/6170_gradebook_example
  • #15: Actually, I lied when I said there were only two modules. There is another tiny little module which actually makes use of the GradeWidget and the GradeBook. You can consider it part of the view, but none of the other modules depend on it. Note that the model can be tested entirely independently of the view. On this chart, the arrows mean dependencies.
  • #16: There’s also the HTML and CSS files, but they’re not too interesting in this example. Note, though, that the gradewidget has a CSS file that logically belongs in its module together with gradewidget.js.
  • #17: http://www.mit.edu/~ebakke/misc/6170_gradebook_example https://github.com/eirikbakke/6170_gradebook_example/commits/master
  • #19: Runtime checks. Repeat what DNJ said before: you shouldn’t be checking for types and constraints everywhere, but you should absolutely check them at API boundaries. Especially when supplied objects go into the model state and errors may not otherwise be discovered until much later.
  • #20: Mention Object.freeze here.
  • #21: Go and show these unit tests in action. https://www.mit.edu/~ebakke/misc/6170_gradebook_example/02_robust/tests.html Spend about 10 minutes on QUnit tests. Point out the “unit” part of unit tests. They’re testing a unit, that is to say, they are only testing a small piece of the program in isolation. Unit tests are most valuable when written early in the development process; that’s when they will best help you catch bugs, especially as you make a lot of changes to your application.
  • #22: Point out that I’m using the JSDoc format, with the caveat that it doesn’t quite understand the this = that idiom (with some more annotations it would work). Spend no more than 5 minutes on this.
  • #25: Note here how the GradeWidget takes a domContainer and a model object. It does not install itself arbitrarily on the page.
  • #26: The main point here is that we are building DOM elements from a model object. There also exists client-side templating libraries that help with this. Point out where we get the grade from—it comes from the model. Constructing DOM “behind the scenes” (good practice). Also discuss the technique of regenerating the entire view every time there is an update (less complexity in code, but can upset UI state such as scroll position, focus, and tooltip state).
  • #27: Don’t go into too much detail here; just point out that as part of our refresh, we assign a click handler to each grade, and put an input field in the right position if any grade has previously been clicked. About detach(): jQuery will automatically detach any listeners when an element is removed from the DOM. Use detach() to remove gradeInputElm without removing its listeners, since we'll be reusing it.
  • #29: The most important point here is that the setting of the grade is done on the model, not somewhere in the view. The view gets updated in turn when rebuild_table() is called.
  • #30: There’s also a little bit of CSS here.
  • #31: Ask the class what is wrong. Get a good answer before moving on.
  • #32: Tell students not to look at the solution yet. http://www.mit.edu/~ebakke/misc/6170_gradebook_example/challenge.zip http://tinyurl.com/pq9gm8v Might need more than 5 minutes on this one; though students can continue to try to implement the solution as I move along.
  • #34: Stress that we are able to avoid making the model dependent on the view. Compare subscribers to event handlers.
  • #37: We can generalize the GradeWidget a bit so that grades can be displayed either by assignment or by student. That way we can show off a useful reason why you might want two widgets based on the same model. (The exact logic in this part of the example is not so important.)
  • #40: A Game of Life solution organized like this can be done in less than 300 lines of JavaScript (+tests).