SlideShare une entreprise Scribd logo
1  sur  38
Télécharger pour lire hors ligne
JS AND PATTERNS
or: How I Learned to Stop Worrying and Love JS Frameworks
@drpicox
1
Sunday 25 May 14
PATTERNS
ModelView Controller
+
High Cohesion & Low Coupling
2 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
3 @drpicox
tree
with apples
http://clipart.nicubunu.ro
Sunday 25 May 14
MODELVIEW CONTROLLER
4 @drpicox
Models
ViewsControllers
Sunday 25 May 14
MODELVIEW CONTROLLER
Model: is almost data
user = {
id: ‘123’,
name: ‘John’,
surname: ‘Smith’,
fullName: function() {
return this.name+‘ ’+this.surname;
},
...
};
5 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
Model: it might be also some view state
tabChooser = {
tabs: [‘introduction’,‘advanced’],
current: 0,
next: function() {
this.current += 1;
this.current %= this.tabs.length;
},
...
};
6 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
Model: knows nothing aboutViews and Controllers
(it is observed: callbacks, dirtycheck, ...)
user.addObserver(userFormView)
ex: look for Observer pattern for more info.
7 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
View: is almost html/css
<form name=“user”>
<label for=“name”>Name:</label>
<input name=“name” type=“text”>
<label for=“surname”>Surname:</label>
<input name=“surname” type=“text”>
<input type=“submit” value=“Update”>
</form>
8 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
View: + a little bit of JS
userFormView = {
setUser: function(user) {
user.addObserver(this);
this.user = user;
this.update();
},
update: function() {
this.form.name = user.name;
this.form.surname = surname;
},
...
}; 9 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
View: knows nothing about Controllers
(it is used: callbacks, interfaces, ...)
userFormView.onSubmit(controller.save)
10 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
Controller: is almost everything else
user = http.get(‘/user’);
userFormView = new UserFormView();
userFormView.setUser(user);
userFormView.onSubmit(function() {
http.post(‘/user’, user);
});
viewport.setView(userFormView);
11 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
Controller: is almost everything else
user = http.get(‘/user’);
userFormView = new UserFormView();
userFormView.setUser(user);
userFormView.onSubmit(function() {
http.post(‘/user’, user);
});
viewport.setView(userFormView);
Be Careful!
Persistence/Service
Controller
loads an stores data (may use identity map)
12 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
Controller: is almost everything else
user = http.get(‘/user’);
userFormView = new UserFormView();
userFormView.setUser(user);
userFormView.onSubmit(function() {
http.post(‘/user’, user);
});
viewport.setView(userFormView);
Be Careful!
Route
Controller
ex: http://localhost:8080/#/user
13 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
Controller: is almost everything else
user = http.get(‘/user’);
userFormView = new UserFormView();
userFormView.setUser(user);
userFormView.onSubmit(function() {
http.post(‘/user’, user);
});
viewport.setView(userFormView);
Be Careful!
View
Controller
handle an specific view
14 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
Controller: detect controllers that you need
• Persistence/Service Controllers: load, store, manages models.
Uses other persistence controllers and models
• View Controllers: prepare and observe views.
Uses persistence controllers, views and (low) models.
• Route Controllers: select a view to show.
Uses other controllers, (instance) views and (low) models.
15 @drpicox
Sunday 25 May 14
MODELVIEW CONTROLLER
MVC Is NOT a Fashion!
, or ModelView Adapter, or ModelViewViewModel, or ModelView Presenter
Use them wisely
Do not create ternaries of ModelX-ViewX-ControllerX just to follow
the pattern. Create models, views and controllers because they make
sense, and reuse them!
16 @drpicox
Sunday 25 May 14
COMMON SENSE PATTERNS
17 @drpicox
High Cohesion: put related things in the same place
• Persistence Controllers: load/store models of one kind,
• Route Controllers: prepare a view to show,
• View: shows a related information to a user (not a model!),
• Model: describes an entity,
• ...
Sunday 25 May 14
COMMON SENSE PATTERNS
18 @drpicox
Low Coupling: minimize what each part knows of other
• Create direction:A knows B, but B must not know A
Models
Views
View
Controllers
Service
Controllers
Knows
A
B
Sunday 25 May 14
COMMON SENSE PATTERNS
19 @drpicox
They are the most basic pattern to follow,
many other patterns come from them,
even ModelView Controller.
High Cohesion & Low Coupling
Sunday 25 May 14
20
Sunday 25 May 14
MODELVIEW CONTROLLER
Model: is not html/dom
<input name=“name” value=“John”>
<img class=“status” src=“success.jpg”>
21 @drpicox
Model: is a Javascript object
user = {name: “John”};
status = “success”;
Please, don’t
Sunday 25 May 14
22
SO...
ModelView Controller: use it wisely
High Cohesion and Low Coupling: make it your common sense
@drpicox
Sunday 25 May 14
Thanks
23 @drpicoxhttps://github.com/drpicox/tutorial-js-patterns
Code examples:
Sunday 25 May 14
Deeper in code
24 @drpicox
Sunday 25 May 14
DEEPER CODE
Persistence Controller: remote data accesses (v1)
userService = {
get: function() {
return http.get(‘/user’);
},
save: function(user) {
http.post(‘/user’, user);
},
};
25 @drpicox
Simple get and save, of
data.
Sunday 25 May 14
DEEPER CODE
Persistence Controller: remote data accesses (v2)
userService = {
get: function() {
return $http.get(‘/user’).
then(function (response) {
var user = new User(response.data);
return user;
});
},
save: function(user) {
return $http.post(‘/user’, user);
},
}; 26 @drpicox
Recover data and create
model instances.
Sunday 25 May 14
DEEPER CODE
Persistence Controller: remote data accesses (v3)
instance = null;
userService = {
get: function() {
if (instance) { return instance; }
return instance = $http.get(‘/user’).
then(function (response) {
var user = new User(response.data);
return user;
});
},
...
}; 27 @drpicox
Keep track of recovered
instances. Not double
loading, always same
instance for same data.
(have key? Identity map)
Sunday 25 May 14
DEEPER CODE
View Controller: it makes the view alive
function UserFormController(view, user) {
view.user = user;
view.save = function() {
userService.save(user);
};
}
28 @drpicox
For Java programmers: imagine that view satisfies a interface.
For AngularJS programmers: view is indeed $scope.
Sunday 25 May 14
DEEPER CODE
Route Controller: it shows what user wants (v1)
routeManager.when(‘/user’, function() {
var user = userService.get();
var view = new UserFormView();
var controller = new UserFormController(view, user);
viewport.setView(view);
});
29 @drpicox
Sunday 25 May 14
DEEPER CODE
Route Controller: it shows what user wants (v2)
routeManager.when(‘/user’, function() {
userService.get().
then(function (user) {
var view = new UserFormView();
var controller = new UserFormController(view, user);
viewport.setView(view);
});
});
30 @drpicox
Wait for it :-)
Sunday 25 May 14
DEEPER CODE
View: jQuery version
// this{ user, save }
function UserFormView() {
var view = this;
view.template = ‘userForm.tpl.html’;
view.link = function(dom) {
$(‘input[name=“name”]’).attr(‘value’,view.user.name);
$(‘form’).submit(function() {
view.save();
});
};
...
} 31 @drpicox
Sunday 25 May 14
DEEPER CODE
View:AngularJS version
<form ng-submit=“save()”>
<label>Name:</label>
<input ng-model=“user.name” type=“text”>
<label>Surname:</label>
<input ng-model=“user.surname” name=“surname” type=“text”>
<input type=“submit” value=“Update”>
</form>
32 @drpicox
Sunday 25 May 14
COMMON SENSE PATTERNS
@drpicox
Low Coupling: minimize what each part knows of other
• Create direction:A knows B, but B must not know A
Knows
A
B
A
B
C
No circles!
Sunday 25 May 14
When to use them?
34 @drpicox
Sunday 25 May 14
Multiple Page Application Single Page Application
• JS controls the application flow
• SEO requires pre-render
• Server delivers assets and API
• Application session is in JS
• JS framework recommended
• PHP/JSP/RoR/... controls flow
• SEO oriented
• Server delivers anything
• Application session is in Server
• Just jQuery
35 @drpicox
Sunday 25 May 14
Multiple colors and sizes to choose
Price and other info may change
36 @drpicox
www.desigual.com
Sunday 25 May 14
No one wants to wait here
37 @drpicox
www.desigual.com
Sunday 25 May 14
So...
Multiple Page Applications also requires some JS framework
38 @drpicox
Sunday 25 May 14

Contenu connexe

Tendances

Maintainable JavaScript 2011
Maintainable JavaScript 2011Maintainable JavaScript 2011
Maintainable JavaScript 2011Nicholas Zakas
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testingjeresig
 
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski
 
Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214David Rodenas
 
Inside PyMongo - MongoNYC
Inside PyMongo - MongoNYCInside PyMongo - MongoNYC
Inside PyMongo - MongoNYCMike Dirolf
 
Easy tests with Selenide and Easyb
Easy tests with Selenide and EasybEasy tests with Selenide and Easyb
Easy tests with Selenide and EasybIakiv Kramarenko
 
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)Danny Preussler
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Anton Arhipov
 
10 Typical Problems in Enterprise Java Applications
10 Typical Problems in Enterprise Java Applications10 Typical Problems in Enterprise Java Applications
10 Typical Problems in Enterprise Java ApplicationsEberhard Wolff
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript TestingKissy Team
 
Android programming -_pushing_the_limits
Android programming -_pushing_the_limitsAndroid programming -_pushing_the_limits
Android programming -_pushing_the_limitsDroidcon Berlin
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013Michelangelo van Dam
 
Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Michelangelo van Dam
 
Demystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDemystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDanny Preussler
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languagesRafael Winterhalter
 
Redux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncRedux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncArtur Szott
 

Tendances (20)

Maintainable JavaScript 2011
Maintainable JavaScript 2011Maintainable JavaScript 2011
Maintainable JavaScript 2011
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, howTomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
 
Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214
 
Inside PyMongo - MongoNYC
Inside PyMongo - MongoNYCInside PyMongo - MongoNYC
Inside PyMongo - MongoNYC
 
Easy tests with Selenide and Easyb
Easy tests with Selenide and EasybEasy tests with Selenide and Easyb
Easy tests with Selenide and Easyb
 
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
 
10 Typical Problems in Enterprise Java Applications
10 Typical Problems in Enterprise Java Applications10 Typical Problems in Enterprise Java Applications
10 Typical Problems in Enterprise Java Applications
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Android programming -_pushing_the_limits
Android programming -_pushing_the_limitsAndroid programming -_pushing_the_limits
Android programming -_pushing_the_limits
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
Easy Button
Easy ButtonEasy Button
Easy Button
 
UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013
 
Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013
 
Demystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDemystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and Toothpick
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languages
 
Redux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncRedux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with Async
 

Similaire à JS and patterns

Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverSpike Brehm
 
Cursor injection
Cursor injectionCursor injection
Cursor injectionfangjiafu
 
Spring mvc my Faviourite Slide
Spring mvc my Faviourite SlideSpring mvc my Faviourite Slide
Spring mvc my Faviourite SlideDaniel Adenew
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep diveAxilis
 
What's Coming in Spring 3.0
What's Coming in Spring 3.0What's Coming in Spring 3.0
What's Coming in Spring 3.0Matt Raible
 
Built to last javascript for enterprise
Built to last   javascript for enterpriseBuilt to last   javascript for enterprise
Built to last javascript for enterpriseMarjan Nikolovski
 
ProjectApp_CodeDataLayer.csusing System;using System.Coll.docx
ProjectApp_CodeDataLayer.csusing System;using System.Coll.docxProjectApp_CodeDataLayer.csusing System;using System.Coll.docx
ProjectApp_CodeDataLayer.csusing System;using System.Coll.docxbriancrawford30935
 
Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Matt Raible
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For ManagersAgileThought
 
Catalyst patterns-yapc-eu-2016
Catalyst patterns-yapc-eu-2016Catalyst patterns-yapc-eu-2016
Catalyst patterns-yapc-eu-2016John Napiorkowski
 
Apple Templates Considered Harmful
Apple Templates Considered HarmfulApple Templates Considered Harmful
Apple Templates Considered HarmfulBrian Gesiak
 
TYPO3 Flow 2.0 (T3CON13 San Francisco)
TYPO3 Flow 2.0 (T3CON13 San Francisco)TYPO3 Flow 2.0 (T3CON13 San Francisco)
TYPO3 Flow 2.0 (T3CON13 San Francisco)Robert Lemke
 

Similaire à JS and patterns (20)

Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
 
Cursor injection
Cursor injectionCursor injection
Cursor injection
 
Spring mvc my Faviourite Slide
Spring mvc my Faviourite SlideSpring mvc my Faviourite Slide
Spring mvc my Faviourite Slide
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep dive
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
What's Coming in Spring 3.0
What's Coming in Spring 3.0What's Coming in Spring 3.0
What's Coming in Spring 3.0
 
Built to last javascript for enterprise
Built to last   javascript for enterpriseBuilt to last   javascript for enterprise
Built to last javascript for enterprise
 
ProjectApp_CodeDataLayer.csusing System;using System.Coll.docx
ProjectApp_CodeDataLayer.csusing System;using System.Coll.docxProjectApp_CodeDataLayer.csusing System;using System.Coll.docx
ProjectApp_CodeDataLayer.csusing System;using System.Coll.docx
 
Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For Managers
 
Catalyst patterns-yapc-eu-2016
Catalyst patterns-yapc-eu-2016Catalyst patterns-yapc-eu-2016
Catalyst patterns-yapc-eu-2016
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
What is MVC?
What is MVC?What is MVC?
What is MVC?
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Apple Templates Considered Harmful
Apple Templates Considered HarmfulApple Templates Considered Harmful
Apple Templates Considered Harmful
 
TYPO3 Flow 2.0 (T3CON13 San Francisco)
TYPO3 Flow 2.0 (T3CON13 San Francisco)TYPO3 Flow 2.0 (T3CON13 San Francisco)
TYPO3 Flow 2.0 (T3CON13 San Francisco)
 

Plus de David Rodenas

TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDDavid Rodenas
 
TDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingTDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingDavid Rodenas
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesDavid Rodenas
 
Be professional: We Rule the World
Be professional: We Rule the WorldBe professional: We Rule the World
Be professional: We Rule the WorldDavid Rodenas
 
ES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorDavid Rodenas
 
ES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataDavid Rodenas
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersBasic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersDavid Rodenas
 
From high school to university and work
From high school to university and workFrom high school to university and work
From high school to university and workDavid Rodenas
 
Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1David Rodenas
 
Freelance i Enginyeria
Freelance i EnginyeriaFreelance i Enginyeria
Freelance i EnginyeriaDavid Rodenas
 
Angular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsAngular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsDavid Rodenas
 
Mvc - Model: the great forgotten
Mvc - Model: the great forgottenMvc - Model: the great forgotten
Mvc - Model: the great forgottenDavid Rodenas
 
(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and backDavid Rodenas
 
Testing: ¿what, how, why?
Testing: ¿what, how, why?Testing: ¿what, how, why?
Testing: ¿what, how, why?David Rodenas
 

Plus de David Rodenas (18)

TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDD
 
TDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingTDD CrashCourse Part1: Testing
TDD CrashCourse Part1: Testing
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD Techniques
 
Be professional: We Rule the World
Be professional: We Rule the WorldBe professional: We Rule the World
Be professional: We Rule the World
 
ES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD Calculator
 
ES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game Kata
 
ES3-2020-05 Testing
ES3-2020-05 TestingES3-2020-05 Testing
ES3-2020-05 Testing
 
Vespres
VespresVespres
Vespres
 
Faster web pages
Faster web pagesFaster web pages
Faster web pages
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersBasic Tutorial of React for Programmers
Basic Tutorial of React for Programmers
 
From high school to university and work
From high school to university and workFrom high school to university and work
From high school to university and work
 
Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1
 
Freelance i Enginyeria
Freelance i EnginyeriaFreelance i Enginyeria
Freelance i Enginyeria
 
Angular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsAngular 1.X Community and API Decissions
Angular 1.X Community and API Decissions
 
MVS: An angular MVC
MVS: An angular MVCMVS: An angular MVC
MVS: An angular MVC
 
Mvc - Model: the great forgotten
Mvc - Model: the great forgottenMvc - Model: the great forgotten
Mvc - Model: the great forgotten
 
(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back
 
Testing: ¿what, how, why?
Testing: ¿what, how, why?Testing: ¿what, how, why?
Testing: ¿what, how, why?
 

Dernier

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 

Dernier (20)

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

JS and patterns

  • 1. JS AND PATTERNS or: How I Learned to Stop Worrying and Love JS Frameworks @drpicox 1 Sunday 25 May 14
  • 2. PATTERNS ModelView Controller + High Cohesion & Low Coupling 2 @drpicox Sunday 25 May 14
  • 3. MODELVIEW CONTROLLER 3 @drpicox tree with apples http://clipart.nicubunu.ro Sunday 25 May 14
  • 5. MODELVIEW CONTROLLER Model: is almost data user = { id: ‘123’, name: ‘John’, surname: ‘Smith’, fullName: function() { return this.name+‘ ’+this.surname; }, ... }; 5 @drpicox Sunday 25 May 14
  • 6. MODELVIEW CONTROLLER Model: it might be also some view state tabChooser = { tabs: [‘introduction’,‘advanced’], current: 0, next: function() { this.current += 1; this.current %= this.tabs.length; }, ... }; 6 @drpicox Sunday 25 May 14
  • 7. MODELVIEW CONTROLLER Model: knows nothing aboutViews and Controllers (it is observed: callbacks, dirtycheck, ...) user.addObserver(userFormView) ex: look for Observer pattern for more info. 7 @drpicox Sunday 25 May 14
  • 8. MODELVIEW CONTROLLER View: is almost html/css <form name=“user”> <label for=“name”>Name:</label> <input name=“name” type=“text”> <label for=“surname”>Surname:</label> <input name=“surname” type=“text”> <input type=“submit” value=“Update”> </form> 8 @drpicox Sunday 25 May 14
  • 9. MODELVIEW CONTROLLER View: + a little bit of JS userFormView = { setUser: function(user) { user.addObserver(this); this.user = user; this.update(); }, update: function() { this.form.name = user.name; this.form.surname = surname; }, ... }; 9 @drpicox Sunday 25 May 14
  • 10. MODELVIEW CONTROLLER View: knows nothing about Controllers (it is used: callbacks, interfaces, ...) userFormView.onSubmit(controller.save) 10 @drpicox Sunday 25 May 14
  • 11. MODELVIEW CONTROLLER Controller: is almost everything else user = http.get(‘/user’); userFormView = new UserFormView(); userFormView.setUser(user); userFormView.onSubmit(function() { http.post(‘/user’, user); }); viewport.setView(userFormView); 11 @drpicox Sunday 25 May 14
  • 12. MODELVIEW CONTROLLER Controller: is almost everything else user = http.get(‘/user’); userFormView = new UserFormView(); userFormView.setUser(user); userFormView.onSubmit(function() { http.post(‘/user’, user); }); viewport.setView(userFormView); Be Careful! Persistence/Service Controller loads an stores data (may use identity map) 12 @drpicox Sunday 25 May 14
  • 13. MODELVIEW CONTROLLER Controller: is almost everything else user = http.get(‘/user’); userFormView = new UserFormView(); userFormView.setUser(user); userFormView.onSubmit(function() { http.post(‘/user’, user); }); viewport.setView(userFormView); Be Careful! Route Controller ex: http://localhost:8080/#/user 13 @drpicox Sunday 25 May 14
  • 14. MODELVIEW CONTROLLER Controller: is almost everything else user = http.get(‘/user’); userFormView = new UserFormView(); userFormView.setUser(user); userFormView.onSubmit(function() { http.post(‘/user’, user); }); viewport.setView(userFormView); Be Careful! View Controller handle an specific view 14 @drpicox Sunday 25 May 14
  • 15. MODELVIEW CONTROLLER Controller: detect controllers that you need • Persistence/Service Controllers: load, store, manages models. Uses other persistence controllers and models • View Controllers: prepare and observe views. Uses persistence controllers, views and (low) models. • Route Controllers: select a view to show. Uses other controllers, (instance) views and (low) models. 15 @drpicox Sunday 25 May 14
  • 16. MODELVIEW CONTROLLER MVC Is NOT a Fashion! , or ModelView Adapter, or ModelViewViewModel, or ModelView Presenter Use them wisely Do not create ternaries of ModelX-ViewX-ControllerX just to follow the pattern. Create models, views and controllers because they make sense, and reuse them! 16 @drpicox Sunday 25 May 14
  • 17. COMMON SENSE PATTERNS 17 @drpicox High Cohesion: put related things in the same place • Persistence Controllers: load/store models of one kind, • Route Controllers: prepare a view to show, • View: shows a related information to a user (not a model!), • Model: describes an entity, • ... Sunday 25 May 14
  • 18. COMMON SENSE PATTERNS 18 @drpicox Low Coupling: minimize what each part knows of other • Create direction:A knows B, but B must not know A Models Views View Controllers Service Controllers Knows A B Sunday 25 May 14
  • 19. COMMON SENSE PATTERNS 19 @drpicox They are the most basic pattern to follow, many other patterns come from them, even ModelView Controller. High Cohesion & Low Coupling Sunday 25 May 14
  • 21. MODELVIEW CONTROLLER Model: is not html/dom <input name=“name” value=“John”> <img class=“status” src=“success.jpg”> 21 @drpicox Model: is a Javascript object user = {name: “John”}; status = “success”; Please, don’t Sunday 25 May 14
  • 22. 22 SO... ModelView Controller: use it wisely High Cohesion and Low Coupling: make it your common sense @drpicox Sunday 25 May 14
  • 24. Deeper in code 24 @drpicox Sunday 25 May 14
  • 25. DEEPER CODE Persistence Controller: remote data accesses (v1) userService = { get: function() { return http.get(‘/user’); }, save: function(user) { http.post(‘/user’, user); }, }; 25 @drpicox Simple get and save, of data. Sunday 25 May 14
  • 26. DEEPER CODE Persistence Controller: remote data accesses (v2) userService = { get: function() { return $http.get(‘/user’). then(function (response) { var user = new User(response.data); return user; }); }, save: function(user) { return $http.post(‘/user’, user); }, }; 26 @drpicox Recover data and create model instances. Sunday 25 May 14
  • 27. DEEPER CODE Persistence Controller: remote data accesses (v3) instance = null; userService = { get: function() { if (instance) { return instance; } return instance = $http.get(‘/user’). then(function (response) { var user = new User(response.data); return user; }); }, ... }; 27 @drpicox Keep track of recovered instances. Not double loading, always same instance for same data. (have key? Identity map) Sunday 25 May 14
  • 28. DEEPER CODE View Controller: it makes the view alive function UserFormController(view, user) { view.user = user; view.save = function() { userService.save(user); }; } 28 @drpicox For Java programmers: imagine that view satisfies a interface. For AngularJS programmers: view is indeed $scope. Sunday 25 May 14
  • 29. DEEPER CODE Route Controller: it shows what user wants (v1) routeManager.when(‘/user’, function() { var user = userService.get(); var view = new UserFormView(); var controller = new UserFormController(view, user); viewport.setView(view); }); 29 @drpicox Sunday 25 May 14
  • 30. DEEPER CODE Route Controller: it shows what user wants (v2) routeManager.when(‘/user’, function() { userService.get(). then(function (user) { var view = new UserFormView(); var controller = new UserFormController(view, user); viewport.setView(view); }); }); 30 @drpicox Wait for it :-) Sunday 25 May 14
  • 31. DEEPER CODE View: jQuery version // this{ user, save } function UserFormView() { var view = this; view.template = ‘userForm.tpl.html’; view.link = function(dom) { $(‘input[name=“name”]’).attr(‘value’,view.user.name); $(‘form’).submit(function() { view.save(); }); }; ... } 31 @drpicox Sunday 25 May 14
  • 32. DEEPER CODE View:AngularJS version <form ng-submit=“save()”> <label>Name:</label> <input ng-model=“user.name” type=“text”> <label>Surname:</label> <input ng-model=“user.surname” name=“surname” type=“text”> <input type=“submit” value=“Update”> </form> 32 @drpicox Sunday 25 May 14
  • 33. COMMON SENSE PATTERNS @drpicox Low Coupling: minimize what each part knows of other • Create direction:A knows B, but B must not know A Knows A B A B C No circles! Sunday 25 May 14
  • 34. When to use them? 34 @drpicox Sunday 25 May 14
  • 35. Multiple Page Application Single Page Application • JS controls the application flow • SEO requires pre-render • Server delivers assets and API • Application session is in JS • JS framework recommended • PHP/JSP/RoR/... controls flow • SEO oriented • Server delivers anything • Application session is in Server • Just jQuery 35 @drpicox Sunday 25 May 14
  • 36. Multiple colors and sizes to choose Price and other info may change 36 @drpicox www.desigual.com Sunday 25 May 14
  • 37. No one wants to wait here 37 @drpicox www.desigual.com Sunday 25 May 14
  • 38. So... Multiple Page Applications also requires some JS framework 38 @drpicox Sunday 25 May 14