SlideShare a Scribd company logo
1 of 33
Implementing MVVM in JavaScript with Knockout
Knockout
JS
Who am I?
• Andoni Arroyo
Development Advisor at Plain Concepts.
• Blog
http://geeks.ms/blogs/aarroyo/
• Web
http://www.andoniarroyo.com/
• Twitter
@andoniarroyo
• LinkedIn
http://www.linkedin.com/in/andoniarroyo
Agenda
• Why I need a pattern?
• MVVM (Model View ViewModel) and The Benefits of MVVM
• Knockout JS
• Connecting View Models to Views, Computed observables, Observable Arrays
• Control-Flow Bindings
• Interactive Bindings
• Accessing External Data
• The template binding
• Some common techniques…
• Custom bindings
Why I need a pattern?
• Complexity
• Rightsizing
• Maintain your code
• You are not alone! (diferents profiles)
• Testeability
• UX
• Avoiding roundtrips
• SPA (Web, mobile)
Ok, we need a plan…
• A pattern is a plan!
• All the profiles know where set and get the elements.
• Take in mind Project requeriments.
MVVM (Model View ViewModel)
• Architectural pattern
• Based on MVC and MVP
• Separate:
• User-interfaces (UI).
• Behavior of the application.
(State and workflow).
• Business logic.
Model
• Implementation of the application's domain model.
• Data model along with business and validation logic.
• Repositories, business objects, data transfer objects (DTOs), Plain Old CLR
Objects (POCOs), and generated entity and proxy objects.
View
• Visual representation of that data
Structure, layout, and appearance of what the user sees on the
screen.
• Receives the user’s interaction.
View Model
• Intermediary between the view and the model, and is responsible for
handling the view logic.
• Provides data from the model in a form that the view can easily use.
• Provides implementations of commands that a user of the application
initiates in the view.
• Defining logical state changes that affect some aspect of the display in the
view.
The Benefits of MVVM
• Separation Separation of concerns
• Developers and designers can work more independently and concurrently on
their components.
• Decoupled pieces.
• Maintenible code (More testeable and easy to understand)
Knockout JS
• JavaScript library:
• Declarative Bindings
Easily associate DOM elements with model data using a concise, readable
syntax.
• Automatic UI Refresh
When your data model's state changes, your UI updates automatically.
• Dependency Tracking
Implicitly set up chains of relationships between model data, to transform and
combine it.
• Templating
Quickly generate sophisticated, nested UIs as a function of your model data.
Knockout JS (Some features)
• No dependencies
• Browser support:
• Mozilla Firefox 2.0+ (latest version tested = 3.6.8)
• Google Chrome (tested on version 5 for Windows and Mac; should work on older
versions too)
• Microsoft Internet Explorer 6, 7, 8, 9, 10
• Apple Safari (tested on Windows Safari version 5, Mac OS X Safari version 3.1.2, and
iPhone Safari for iOS 4; should work on newer/older versions too).
• Opera 10 for Windows.
• Don’t recheck these for every release:
• Opera Mini
• Google Android OS browser (OS version 2.2)
Connecting Views to View Models
• Observables
JavaScript variables but let Knockout observe their changes and
automatically update the relevant parts of the view.
• Bindings (data-bind)
Connect a user interface component in the view to a particular
observable, you have to bind an HTML element to it.
• ko.applyBindings
Create a relation between the View and the source object.
Demo
• Demo 1 - Connecting Views to View Models
Computed observables
• Properties that are dynamically generated.
Let combine several normal observables into a single property, and
Knockout will still keep the view up-to-date whenever any of the
underlying values change.
Demo
• Demo 2 - Computed observables
Observable Arrays
• Let track lists of items.
• Implementation of the common methods:
push() pop() unshift()
shift() slice() remove()
removeAll() destroy() destroyAll()
sort() reversed() indexOf()
Control-Flow Bindings
• The foreach binding
Iterates through the array and uses each item it finds for the binding
context of the contained markup.
• Binding context ($root, $data, $index, $parent)
• Events (afterRender, afterAdd, beforeRemove, beforeMove, afterMove)
Control-Flow Bindings II
• if and ifnot
If the parameter you pass evaluates to true, the contained HTML will
be displayed, otherwise it’s removed from the DOM.
• with
Used to manually declare the scope of a particular block.
Demo
• Demo 3 - Control-Flow Bindings
Interactive Bindings
• click: <method>
Call a ViewModel method when the element is clicked.
• value: <property>
Link a form element’s value to a ViewModel property.
• enable: <property>/ disable: <property>
Enable/Disabled an element based on a certain condition.
• checked: <property>
Link a radio button or check box to a ViewModel property.
Interactive Bindings II
• options: <array>
Define a <select> element with a ViewModel array.
• selectedOptions: <array>
Define the active elements in a <select> field.
• event: <object>
Call a method when a user-initiated event occurs.
• submit: <method>
Call a method when a form is submitted.
• hasfocus: <property>
Define whether or not the element is focused
Demo
• Demo 4 - Interactive Bindings
Accessing External Data
• Load ViewModel from JSON
ko.mapping.fromJS
• Converting View Model Data to Plain JSON
ko.mapping.toJS
• knockout.mapping plugin
Demo
• Demo 5 - Accessing External Data
The template binding
• Out of the box “inline”
• External templates
• Knockout.js External Template Engine
• https://github.com/ifandelse/Knockout.js-External-Template-Engine
(nuget)
• https://nuget.org/packages/Knockout.js_External_Template_Engine
Demo
• Demo 6 - Templates
Some common techniques…
• Subscribe to observables.
• The "throttle" extender.
• re-evaluation until its dependencies have stopped changing for a specified
period of time
Demo
• Demo 7 - Common techniques
Custom bindings
• The way to extend Knockout
• ko.bindingHandlers
• init
Once for each DOM element that you use the binding on.
• set any initial state
• register any event handlers
• update
The associated observable has changed
Demo
• Demo 7 – Creating customBinding
Questions?
Thank you
• Andoni Arroyo
• andoni.arroyo@gmail.com
• Blog
http://geeks.ms/blogs/aarroyo/
• Web
http://www.andoniarroyo.com/
• Twitter
@andoniarroyo
• LinkedIn
http://www.linkedin.com/in/andoniarroyo

More Related Content

What's hot

Angular JS tutorial
Angular JS tutorialAngular JS tutorial
Angular JS tutorialcncwebworld
 
Introduction of angular js
Introduction of angular jsIntroduction of angular js
Introduction of angular jsTamer Solieman
 
An introduction to knockout.js
An introduction to knockout.jsAn introduction to knockout.js
An introduction to knockout.jsEmanuele DelBono
 
Learning AngularJS - Complete coverage of AngularJS features and concepts
Learning AngularJS  - Complete coverage of AngularJS features and conceptsLearning AngularJS  - Complete coverage of AngularJS features and concepts
Learning AngularJS - Complete coverage of AngularJS features and conceptsSuresh Patidar
 
Angular js for beginners
Angular js for beginnersAngular js for beginners
Angular js for beginnersMunir Hoque
 
Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)Ruud van Falier
 
Sexy Architecting. VIPER: MVP on steroids
Sexy Architecting. VIPER: MVP on steroidsSexy Architecting. VIPER: MVP on steroids
Sexy Architecting. VIPER: MVP on steroidsDmytro Zaitsev
 
Single page application 03
Single page application   03Single page application   03
Single page application 03Ismaeel Enjreny
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS introdizabl
 
Getting Started with Angular JS
Getting Started with Angular JSGetting Started with Angular JS
Getting Started with Angular JSAkshay Mathur
 
Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsallanh0526
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014Sarah Hudson
 
Knockoutjs databinding
Knockoutjs databindingKnockoutjs databinding
Knockoutjs databindingBoulos Dib
 
KnockOutjs from Scratch
KnockOutjs from ScratchKnockOutjs from Scratch
KnockOutjs from ScratchUdaya Kumar
 

What's hot (20)

Angular JS tutorial
Angular JS tutorialAngular JS tutorial
Angular JS tutorial
 
Introduction of angular js
Introduction of angular jsIntroduction of angular js
Introduction of angular js
 
An introduction to knockout.js
An introduction to knockout.jsAn introduction to knockout.js
An introduction to knockout.js
 
Asp.Net MVC 5 in Arabic
Asp.Net MVC 5 in ArabicAsp.Net MVC 5 in Arabic
Asp.Net MVC 5 in Arabic
 
Learning AngularJS - Complete coverage of AngularJS features and concepts
Learning AngularJS  - Complete coverage of AngularJS features and conceptsLearning AngularJS  - Complete coverage of AngularJS features and concepts
Learning AngularJS - Complete coverage of AngularJS features and concepts
 
Angular js for beginners
Angular js for beginnersAngular js for beginners
Angular js for beginners
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
 
Viper architecture
Viper architectureViper architecture
Viper architecture
 
Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
Sexy Architecting. VIPER: MVP on steroids
Sexy Architecting. VIPER: MVP on steroidsSexy Architecting. VIPER: MVP on steroids
Sexy Architecting. VIPER: MVP on steroids
 
Angular js
Angular jsAngular js
Angular js
 
Single page application 03
Single page application   03Single page application   03
Single page application 03
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS intro
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Getting Started with Angular JS
Getting Started with Angular JSGetting Started with Angular JS
Getting Started with Angular JS
 
Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS apps
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014
 
Knockoutjs databinding
Knockoutjs databindingKnockoutjs databinding
Knockoutjs databinding
 
KnockOutjs from Scratch
KnockOutjs from ScratchKnockOutjs from Scratch
KnockOutjs from Scratch
 

Viewers also liked

TOOLs for business management #bookbox
TOOLs for business management #bookbox TOOLs for business management #bookbox
TOOLs for business management #bookbox Vika Zhurbas
 
Bibliotēkas bērnu stūrītis
Bibliotēkas bērnu stūrītisBibliotēkas bērnu stūrītis
Bibliotēkas bērnu stūrītisberbib
 
The Linkedin Follower Ecosystem
The Linkedin Follower EcosystemThe Linkedin Follower Ecosystem
The Linkedin Follower EcosystemDennie Cuypers
 
W.T.F. IS “THIS”……In Javascript?
W.T.F. IS “THIS”……In Javascript?W.T.F. IS “THIS”……In Javascript?
W.T.F. IS “THIS”……In Javascript?Andoni Arroyo
 

Viewers also liked (6)

TOOLs for business management #bookbox
TOOLs for business management #bookbox TOOLs for business management #bookbox
TOOLs for business management #bookbox
 
powerpoint
powerpointpowerpoint
powerpoint
 
Podbean
PodbeanPodbean
Podbean
 
Bibliotēkas bērnu stūrītis
Bibliotēkas bērnu stūrītisBibliotēkas bērnu stūrītis
Bibliotēkas bērnu stūrītis
 
The Linkedin Follower Ecosystem
The Linkedin Follower EcosystemThe Linkedin Follower Ecosystem
The Linkedin Follower Ecosystem
 
W.T.F. IS “THIS”……In Javascript?
W.T.F. IS “THIS”……In Javascript?W.T.F. IS “THIS”……In Javascript?
W.T.F. IS “THIS”……In Javascript?
 

Similar to Knockout implementing mvvm in java script with knockout

MV* presentation frameworks in Javascript: en garde, pret, allez!
MV* presentation frameworks in Javascript: en garde, pret, allez!MV* presentation frameworks in Javascript: en garde, pret, allez!
MV* presentation frameworks in Javascript: en garde, pret, allez!Roberto Messora
 
Building databound JavaScript apps with Knockoutjs
Building databound JavaScript apps with KnockoutjsBuilding databound JavaScript apps with Knockoutjs
Building databound JavaScript apps with KnockoutjsNish Anil
 
Angular JS, A dive to concepts
Angular JS, A dive to conceptsAngular JS, A dive to concepts
Angular JS, A dive to conceptsAbhishek Sur
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applicationsIvano Malavolta
 
Backbone JS for mobile apps
Backbone JS for mobile appsBackbone JS for mobile apps
Backbone JS for mobile appsIvano Malavolta
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesQamar Abbas
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - IntroductionSagar Acharya
 
JavaScript for ASP.NET programmers (webcast) upload
JavaScript for ASP.NET programmers (webcast) uploadJavaScript for ASP.NET programmers (webcast) upload
JavaScript for ASP.NET programmers (webcast) uploadRuss Fustino
 
MVVM Magic in SharePoint 2010 using Knockoutjs!
MVVM Magic in SharePoint 2010 using Knockoutjs!MVVM Magic in SharePoint 2010 using Knockoutjs!
MVVM Magic in SharePoint 2010 using Knockoutjs!jhendrix88
 
ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015Hossein Zahed
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsJennifer Estrada
 
Valentine with Angular js - Introduction
Valentine with Angular js - IntroductionValentine with Angular js - Introduction
Valentine with Angular js - IntroductionSenthil Kumar
 
MVC + ORM (with project implementation)
MVC + ORM (with project implementation)MVC + ORM (with project implementation)
MVC + ORM (with project implementation)Prateek Chauhan
 

Similar to Knockout implementing mvvm in java script with knockout (20)

Training: MVVM Pattern
Training: MVVM PatternTraining: MVVM Pattern
Training: MVVM Pattern
 
MV* presentation frameworks in Javascript: en garde, pret, allez!
MV* presentation frameworks in Javascript: en garde, pret, allez!MV* presentation frameworks in Javascript: en garde, pret, allez!
MV* presentation frameworks in Javascript: en garde, pret, allez!
 
Building databound JavaScript apps with Knockoutjs
Building databound JavaScript apps with KnockoutjsBuilding databound JavaScript apps with Knockoutjs
Building databound JavaScript apps with Knockoutjs
 
Angular JS, A dive to concepts
Angular JS, A dive to conceptsAngular JS, A dive to concepts
Angular JS, A dive to concepts
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applications
 
Backbone JS for mobile apps
Backbone JS for mobile appsBackbone JS for mobile apps
Backbone JS for mobile apps
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
Module2
Module2Module2
Module2
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelines
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
 
JavaScript for ASP.NET programmers (webcast) upload
JavaScript for ASP.NET programmers (webcast) uploadJavaScript for ASP.NET programmers (webcast) upload
JavaScript for ASP.NET programmers (webcast) upload
 
Adopting MVVM
Adopting MVVMAdopting MVVM
Adopting MVVM
 
MVVM Magic in SharePoint 2010 using Knockoutjs!
MVVM Magic in SharePoint 2010 using Knockoutjs!MVVM Magic in SharePoint 2010 using Knockoutjs!
MVVM Magic in SharePoint 2010 using Knockoutjs!
 
ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015
 
[2015/2016] Backbone JS
[2015/2016] Backbone JS[2015/2016] Backbone JS
[2015/2016] Backbone JS
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAs
 
Valentine with Angular js - Introduction
Valentine with Angular js - IntroductionValentine with Angular js - Introduction
Valentine with Angular js - Introduction
 
MVC + ORM (with project implementation)
MVC + ORM (with project implementation)MVC + ORM (with project implementation)
MVC + ORM (with project implementation)
 

Knockout implementing mvvm in java script with knockout

  • 1. Implementing MVVM in JavaScript with Knockout Knockout JS
  • 2. Who am I? • Andoni Arroyo Development Advisor at Plain Concepts. • Blog http://geeks.ms/blogs/aarroyo/ • Web http://www.andoniarroyo.com/ • Twitter @andoniarroyo • LinkedIn http://www.linkedin.com/in/andoniarroyo
  • 3. Agenda • Why I need a pattern? • MVVM (Model View ViewModel) and The Benefits of MVVM • Knockout JS • Connecting View Models to Views, Computed observables, Observable Arrays • Control-Flow Bindings • Interactive Bindings • Accessing External Data • The template binding • Some common techniques… • Custom bindings
  • 4. Why I need a pattern? • Complexity • Rightsizing • Maintain your code • You are not alone! (diferents profiles) • Testeability • UX • Avoiding roundtrips • SPA (Web, mobile)
  • 5. Ok, we need a plan… • A pattern is a plan! • All the profiles know where set and get the elements. • Take in mind Project requeriments.
  • 6. MVVM (Model View ViewModel) • Architectural pattern • Based on MVC and MVP • Separate: • User-interfaces (UI). • Behavior of the application. (State and workflow). • Business logic.
  • 7. Model • Implementation of the application's domain model. • Data model along with business and validation logic. • Repositories, business objects, data transfer objects (DTOs), Plain Old CLR Objects (POCOs), and generated entity and proxy objects.
  • 8. View • Visual representation of that data Structure, layout, and appearance of what the user sees on the screen. • Receives the user’s interaction.
  • 9. View Model • Intermediary between the view and the model, and is responsible for handling the view logic. • Provides data from the model in a form that the view can easily use. • Provides implementations of commands that a user of the application initiates in the view. • Defining logical state changes that affect some aspect of the display in the view.
  • 10. The Benefits of MVVM • Separation Separation of concerns • Developers and designers can work more independently and concurrently on their components. • Decoupled pieces. • Maintenible code (More testeable and easy to understand)
  • 11. Knockout JS • JavaScript library: • Declarative Bindings Easily associate DOM elements with model data using a concise, readable syntax. • Automatic UI Refresh When your data model's state changes, your UI updates automatically. • Dependency Tracking Implicitly set up chains of relationships between model data, to transform and combine it. • Templating Quickly generate sophisticated, nested UIs as a function of your model data.
  • 12. Knockout JS (Some features) • No dependencies • Browser support: • Mozilla Firefox 2.0+ (latest version tested = 3.6.8) • Google Chrome (tested on version 5 for Windows and Mac; should work on older versions too) • Microsoft Internet Explorer 6, 7, 8, 9, 10 • Apple Safari (tested on Windows Safari version 5, Mac OS X Safari version 3.1.2, and iPhone Safari for iOS 4; should work on newer/older versions too). • Opera 10 for Windows. • Don’t recheck these for every release: • Opera Mini • Google Android OS browser (OS version 2.2)
  • 13. Connecting Views to View Models • Observables JavaScript variables but let Knockout observe their changes and automatically update the relevant parts of the view. • Bindings (data-bind) Connect a user interface component in the view to a particular observable, you have to bind an HTML element to it. • ko.applyBindings Create a relation between the View and the source object.
  • 14. Demo • Demo 1 - Connecting Views to View Models
  • 15. Computed observables • Properties that are dynamically generated. Let combine several normal observables into a single property, and Knockout will still keep the view up-to-date whenever any of the underlying values change.
  • 16. Demo • Demo 2 - Computed observables
  • 17. Observable Arrays • Let track lists of items. • Implementation of the common methods: push() pop() unshift() shift() slice() remove() removeAll() destroy() destroyAll() sort() reversed() indexOf()
  • 18. Control-Flow Bindings • The foreach binding Iterates through the array and uses each item it finds for the binding context of the contained markup. • Binding context ($root, $data, $index, $parent) • Events (afterRender, afterAdd, beforeRemove, beforeMove, afterMove)
  • 19. Control-Flow Bindings II • if and ifnot If the parameter you pass evaluates to true, the contained HTML will be displayed, otherwise it’s removed from the DOM. • with Used to manually declare the scope of a particular block.
  • 20. Demo • Demo 3 - Control-Flow Bindings
  • 21. Interactive Bindings • click: <method> Call a ViewModel method when the element is clicked. • value: <property> Link a form element’s value to a ViewModel property. • enable: <property>/ disable: <property> Enable/Disabled an element based on a certain condition. • checked: <property> Link a radio button or check box to a ViewModel property.
  • 22. Interactive Bindings II • options: <array> Define a <select> element with a ViewModel array. • selectedOptions: <array> Define the active elements in a <select> field. • event: <object> Call a method when a user-initiated event occurs. • submit: <method> Call a method when a form is submitted. • hasfocus: <property> Define whether or not the element is focused
  • 23. Demo • Demo 4 - Interactive Bindings
  • 24. Accessing External Data • Load ViewModel from JSON ko.mapping.fromJS • Converting View Model Data to Plain JSON ko.mapping.toJS • knockout.mapping plugin
  • 25. Demo • Demo 5 - Accessing External Data
  • 26. The template binding • Out of the box “inline” • External templates • Knockout.js External Template Engine • https://github.com/ifandelse/Knockout.js-External-Template-Engine (nuget) • https://nuget.org/packages/Knockout.js_External_Template_Engine
  • 27. Demo • Demo 6 - Templates
  • 28. Some common techniques… • Subscribe to observables. • The "throttle" extender. • re-evaluation until its dependencies have stopped changing for a specified period of time
  • 29. Demo • Demo 7 - Common techniques
  • 30. Custom bindings • The way to extend Knockout • ko.bindingHandlers • init Once for each DOM element that you use the binding on. • set any initial state • register any event handlers • update The associated observable has changed
  • 31. Demo • Demo 7 – Creating customBinding
  • 33. Thank you • Andoni Arroyo • andoni.arroyo@gmail.com • Blog http://geeks.ms/blogs/aarroyo/ • Web http://www.andoniarroyo.com/ • Twitter @andoniarroyo • LinkedIn http://www.linkedin.com/in/andoniarroyo

Editor's Notes

  1. http://jsfiddle.net/TrH3b/