SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Stands for Model-View-ViewModel
Not technology specific
Awesome with data binding!
MVVM is, foremost, a separation pattern
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Basically just a JavaScript object
Represents the data
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
A user friendly presentation of content
The HTML representation layer
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Contains properties, methods and model
model
Holds the behavior and data for the view
var myViewModel = {
name: ko.observable("Johnny"),
lastName: ko.observable("Tordgeman")
};
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Helps create MVVM apps with observables
Knockout is a MVVM JavaScript framework
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
6+ 2+
Separates behavior and structure
Declarative bindings
Observables
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Declarative
Bindings
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Observables are special JavaScript
objects that can notify subscribers about
changes, and can automatically detect
dependencies.
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Observable Computed
Observable
Array
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
<input data-bind="value: name" placeholder="Name" />
Declarative Binding
var myViewModel = {
name: ko.observable("Johnny")
};
Creating Observable
ko.applyBindings(myViewModel);
Binding the ViewModel
to the View
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
<section class="view-list" data-bind="foreach: games">
<article class="article-container-full-width">
<div>
<img data-bind="attr: { src: imageName }"
class="img-polaroid"/>
<span data-bind="text: Title"></span> <br />
Genre: <span data-bind="text: Genre"></span>
</div>
</article>
</section>
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
var firstName = ko.observable('Johnny');
alert(firstName);
alert(firstName());
Function
definition
Works!
firstName('Eyal');
firstName = 'Eyal';
Works!
firstName
became a
string
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
When You
Need a
Value That
Doesn’t
Exist in the
Web Service
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
var prdViewModel = {
id: ko.observable(1),
productPrice: ko.observable(199),
productQty: ko.observable(30),
productCurrency: ko.observable("$")
};
prdViewModel.sallery = ko.computed(function ()
{
return parseInt(this.productQty() *
this.productPrice()) + this.productCurrency();
}, prdViewModel);
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Tracks
which
objects are
in the array
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Text and
appearance
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
<input type="text"
data-bind="enable: allowEdit,
value: price" />
<select data-bind="options: colors,
value: selectedColor,
optionsText: 'name',
optionsValue: 'key'" ></select>
Built into
Knockout
Binding for
Element
Attributes
Multiple
Binding
Expression
s
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
attr checked click css disable
enable event hasfocus html options
optionsText optionsValue selectedOptions style submit
text uniqueName value visible
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
attr checked click css disable
enable event hasfocus html options
optionsText optionsValue selectedOptions style submit
text uniqueName value visible
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
attr checked click css disable
enable event hasfocus html options
optionsText optionsValue selectedOptions style submit
text uniqueName value visible
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
attr checked click css disable
enable event hasfocus html options
optionsText optionsValue selectedOptions style submit
text uniqueName value visible
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
// Whenever the selectedProduct changes, reset the
selectedPrice property
viewmodel.selectedProduct.subscribe(function
() {
viewmodel.selectedPrice(undefined);
}, viewmodel);
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
<p data-bind="if: lines().length > 0">
Total value: <span data-
bind="text: my.formatCurrency(grandTotal())">
</span>
</p>
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
<div class="navbar-inner navbar-secondary">
<div class="btn-group" data-
bind="foreach: router.visibleRoutes">
<a data-bind="attr: { href: hash }, css:
{ active: isActive }, html: caption"
class="btn btn-info"></a>
</div>
</div>
For each visible route…
Bind the route link
Check if CSS should be
active
And its html to the
content of the link
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Knockout provides us with three methods for custom post-
processing logic of our template:
afterRender
afterAdd
beforeRemove
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Just Another
Binding
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
ko.bindingHandlers.yourBindingName = {
init: function (element, valueAccessor,
allBindingsAccessor, viewModel) {
// This will be called when the binding is first
applied to an element
},
update: function (element, valueAccessor,
allBindingsAccessor, viewModel) {
// This will be called once when the binding is
first applied to an element,
// and again whenever the associated observable
changes value.
}
};
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
ko.bindingHandlers.fadeVisible = {
init: function (element, valueAccessor, allBindingsAccessor,
viewModel) {
var shouldDisplay = valueAccessor();
$(element).toggle(shouldDisplay);
},
update: function (element, valueAccessor, allBindingsAccessor,
viewModel) {
var shouldDisplay = valueAccessor(),
allBindings = allBindingsAccessor();
shouldDisplay ? $(element).fadeIn() : $(element).fadeOut()
;
}
};
Bound DOM
element
1 Value passed
to the binding
2 All bindings
on same element
3
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
While Knockout observables provides
the basic features necessary to support
reading/writing values and notifying
subscribers when that value changes,
you may wish to add your own logic to it.
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
ko.extenders.logChange = function (target, option) {
target.subscribe(function (newValue) {
console.log(option + ": " + newValue);
});
return target;
};
this.firstName = ko.observable("Bob").extend({
logChange: "first name" });
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Super easy data binding
Allows for extensibility
Fun!
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
http://jpapa.me/teched13ko
http://johnpapa.net/SPA
http://knockoutjs.com/documentation/introduction.html
SPA with KnockoutJS
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |

Weitere ähnliche Inhalte

Was ist angesagt?

Intro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptIntro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate Javascript
Andrew Lovett-Barron
 

Was ist angesagt? (20)

Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
Js il 2013 breeze.js
Js il 2013 breeze.jsJs il 2013 breeze.js
Js il 2013 breeze.js
 
Coffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JSCoffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JS
 
MVVM with SwiftUI and Combine
MVVM with SwiftUI and CombineMVVM with SwiftUI and Combine
MVVM with SwiftUI and Combine
 
Careers in Java Script and Ajax
Careers in Java Script and AjaxCareers in Java Script and Ajax
Careers in Java Script and Ajax
 
Angular js 1.0-fundamentals
Angular js 1.0-fundamentalsAngular js 1.0-fundamentals
Angular js 1.0-fundamentals
 
Styling Components with JavaScript: MelbCSS Edition
Styling Components with JavaScript: MelbCSS EditionStyling Components with JavaScript: MelbCSS Edition
Styling Components with JavaScript: MelbCSS Edition
 
The Complementarity of React and Web Components
The Complementarity of React and Web ComponentsThe Complementarity of React and Web Components
The Complementarity of React and Web Components
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
Introduction to Android Programming
Introduction to Android ProgrammingIntroduction to Android Programming
Introduction to Android Programming
 
Angular2 and TypeScript
Angular2 and TypeScriptAngular2 and TypeScript
Angular2 and TypeScript
 
Course CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.jsCourse CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.js
 
Tips for Angular Applications
Tips for Angular ApplicationsTips for Angular Applications
Tips for Angular Applications
 
Getting Started with Combine And SwiftUI
Getting Started with Combine And SwiftUIGetting Started with Combine And SwiftUI
Getting Started with Combine And SwiftUI
 
Collecting a Image Label from Crowds Using Amazon Mechanical Turk
Collecting a Image Label from Crowds Using Amazon Mechanical TurkCollecting a Image Label from Crowds Using Amazon Mechanical Turk
Collecting a Image Label from Crowds Using Amazon Mechanical Turk
 
Beyond HTML: Tools for Building Web 2.0 Apps
Beyond HTML: Tools for Building Web 2.0 AppsBeyond HTML: Tools for Building Web 2.0 Apps
Beyond HTML: Tools for Building Web 2.0 Apps
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone Interactivity
 
BackboneJS Training - Giving Backbone to your applications
BackboneJS Training - Giving Backbone to your applicationsBackboneJS Training - Giving Backbone to your applications
BackboneJS Training - Giving Backbone to your applications
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015
 
Intro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptIntro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate Javascript
 

Ähnlich wie Mvvm and KnockoutJS

Ui components - js-il.com
Ui components - js-il.comUi components - js-il.com
Ui components - js-il.com
Eyal Vardi
 
jQuery Mobile Apps
jQuery Mobile AppsjQuery Mobile Apps
jQuery Mobile Apps
Dima Kuzmich
 
Rp 6 session 2 naresh bhatia
Rp 6  session 2 naresh bhatiaRp 6  session 2 naresh bhatia
Rp 6 session 2 naresh bhatia
sapientindia
 

Ähnlich wie Mvvm and KnockoutJS (20)

Spa Architecture with Durandal and Friends
Spa Architecture with Durandal and FriendsSpa Architecture with Durandal and Friends
Spa Architecture with Durandal and Friends
 
Routing in SPA
Routing in SPARouting in SPA
Routing in SPA
 
Ui components - js-il.com
Ui components - js-il.comUi components - js-il.com
Ui components - js-il.com
 
Ui components
Ui componentsUi components
Ui components
 
Asp.NET MVC
Asp.NET MVCAsp.NET MVC
Asp.NET MVC
 
jQuery Mobile apps - js-il.com
jQuery Mobile apps - js-il.comjQuery Mobile apps - js-il.com
jQuery Mobile apps - js-il.com
 
jQuery Mobile Apps
jQuery Mobile AppsjQuery Mobile Apps
jQuery Mobile Apps
 
MVVM Lights
MVVM LightsMVVM Lights
MVVM Lights
 
Data binding
Data bindingData binding
Data binding
 
Knockoutjs databinding
Knockoutjs databindingKnockoutjs databinding
Knockoutjs databinding
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Fundaments of Knockout js
Fundaments of Knockout jsFundaments of Knockout js
Fundaments of Knockout js
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Knockout.js
Knockout.jsKnockout.js
Knockout.js
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
Asp.net mvc training
Asp.net mvc trainingAsp.net mvc training
Asp.net mvc training
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
 
Rp 6 session 2 naresh bhatia
Rp 6  session 2 naresh bhatiaRp 6  session 2 naresh bhatia
Rp 6 session 2 naresh bhatia
 
MVC & backbone.js
MVC & backbone.jsMVC & backbone.js
MVC & backbone.js
 
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!
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Mvvm and KnockoutJS

  • 1. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Israel JavaScript Conference
  • 2. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 3. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Stands for Model-View-ViewModel Not technology specific Awesome with data binding! MVVM is, foremost, a separation pattern
  • 4. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Basically just a JavaScript object Represents the data
  • 5. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | A user friendly presentation of content The HTML representation layer
  • 6. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Contains properties, methods and model model Holds the behavior and data for the view var myViewModel = { name: ko.observable("Johnny"), lastName: ko.observable("Tordgeman") };
  • 7. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 8. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Helps create MVVM apps with observables Knockout is a MVVM JavaScript framework
  • 9. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 10. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 11. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | 6+ 2+ Separates behavior and structure Declarative bindings Observables
  • 12. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Declarative Bindings
  • 13. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 14. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Observables are special JavaScript objects that can notify subscribers about changes, and can automatically detect dependencies.
  • 15. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Observable Computed Observable Array
  • 16. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | <input data-bind="value: name" placeholder="Name" /> Declarative Binding var myViewModel = { name: ko.observable("Johnny") }; Creating Observable ko.applyBindings(myViewModel); Binding the ViewModel to the View
  • 17. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 18. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 19. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | <section class="view-list" data-bind="foreach: games"> <article class="article-container-full-width"> <div> <img data-bind="attr: { src: imageName }" class="img-polaroid"/> <span data-bind="text: Title"></span> <br /> Genre: <span data-bind="text: Genre"></span> </div> </article> </section>
  • 20. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | var firstName = ko.observable('Johnny'); alert(firstName); alert(firstName()); Function definition Works! firstName('Eyal'); firstName = 'Eyal'; Works! firstName became a string
  • 21. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 22. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | When You Need a Value That Doesn’t Exist in the Web Service
  • 23. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | var prdViewModel = { id: ko.observable(1), productPrice: ko.observable(199), productQty: ko.observable(30), productCurrency: ko.observable("$") }; prdViewModel.sallery = ko.computed(function () { return parseInt(this.productQty() * this.productPrice()) + this.productCurrency(); }, prdViewModel);
  • 24. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 25. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Tracks which objects are in the array
  • 26. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 27. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Text and appearance
  • 28. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | <input type="text" data-bind="enable: allowEdit, value: price" /> <select data-bind="options: colors, value: selectedColor, optionsText: 'name', optionsValue: 'key'" ></select> Built into Knockout Binding for Element Attributes Multiple Binding Expression s
  • 29. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | attr checked click css disable enable event hasfocus html options optionsText optionsValue selectedOptions style submit text uniqueName value visible
  • 30. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | attr checked click css disable enable event hasfocus html options optionsText optionsValue selectedOptions style submit text uniqueName value visible
  • 31. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | attr checked click css disable enable event hasfocus html options optionsText optionsValue selectedOptions style submit text uniqueName value visible
  • 32. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | attr checked click css disable enable event hasfocus html options optionsText optionsValue selectedOptions style submit text uniqueName value visible
  • 33. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | // Whenever the selectedProduct changes, reset the selectedPrice property viewmodel.selectedProduct.subscribe(function () { viewmodel.selectedPrice(undefined); }, viewmodel);
  • 34. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | <p data-bind="if: lines().length > 0"> Total value: <span data- bind="text: my.formatCurrency(grandTotal())"> </span> </p>
  • 35. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | <div class="navbar-inner navbar-secondary"> <div class="btn-group" data- bind="foreach: router.visibleRoutes"> <a data-bind="attr: { href: hash }, css: { active: isActive }, html: caption" class="btn btn-info"></a> </div> </div> For each visible route… Bind the route link Check if CSS should be active And its html to the content of the link
  • 36. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 37. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Knockout provides us with three methods for custom post- processing logic of our template: afterRender afterAdd beforeRemove
  • 38. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Just Another Binding
  • 39. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | ko.bindingHandlers.yourBindingName = { init: function (element, valueAccessor, allBindingsAccessor, viewModel) { // This will be called when the binding is first applied to an element }, update: function (element, valueAccessor, allBindingsAccessor, viewModel) { // This will be called once when the binding is first applied to an element, // and again whenever the associated observable changes value. } };
  • 40. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | ko.bindingHandlers.fadeVisible = { init: function (element, valueAccessor, allBindingsAccessor, viewModel) { var shouldDisplay = valueAccessor(); $(element).toggle(shouldDisplay); }, update: function (element, valueAccessor, allBindingsAccessor, viewModel) { var shouldDisplay = valueAccessor(), allBindings = allBindingsAccessor(); shouldDisplay ? $(element).fadeIn() : $(element).fadeOut() ; } }; Bound DOM element 1 Value passed to the binding 2 All bindings on same element 3
  • 41. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 42. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | While Knockout observables provides the basic features necessary to support reading/writing values and notifying subscribers when that value changes, you may wish to add your own logic to it.
  • 43. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | ko.extenders.logChange = function (target, option) { target.subscribe(function (newValue) { console.log(option + ": " + newValue); }); return target; }; this.firstName = ko.observable("Bob").extend({ logChange: "first name" });
  • 44. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 45. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | Super easy data binding Allows for extensibility Fun!
  • 46. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com | http://jpapa.me/teched13ko http://johnpapa.net/SPA http://knockoutjs.com/documentation/introduction.html SPA with KnockoutJS
  • 47. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 48. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |
  • 49. Israel JavaScript Conference | 03 – 6325707 | info@e4d.co.il | www.js-il.com |