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 |

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 |