An Introduction to Knockout.js with Daniel Doezema dan.doezema.com - - PowerPoint PPT Presentation

an introduction to knockout js
SMART_READER_LITE
LIVE PREVIEW

An Introduction to Knockout.js with Daniel Doezema dan.doezema.com - - PowerPoint PPT Presentation

An Introduction to Knockout.js with Daniel Doezema dan.doezema.com Knockout.js Standalone / Pure JavaScript Open Source (MIT license) Wide Browser Support ( IE 6+ / FF 2+ / Chrome / Opera / Safari - Mobile) Framework


slide-1
SLIDE 1

An Introduction to Knockout.js

with Daniel Doezema dan.doezema.com

slide-2
SLIDE 2

Knockout.js

  • Standalone / Pure JavaScript
  • Open Source (MIT license)
  • Wide Browser Support

○ (IE 6+ / FF 2+ / Chrome / Opera / Safari - Mobile)

  • Framework Agnostic
slide-3
SLIDE 3

Best Use Cases

  • Web Apps
  • User Interaction
  • UI / UX Priority
  • AJAX / API Backed
slide-4
SLIDE 4

Benefits

  • Reusability (DRY)
  • View Consistency
  • Concise Code
  • Logic / Responsibility Encapsulation
slide-5
SLIDE 5

Model-View-View Model (MVVM)

  • Model
  • Persistent Data Store
  • View
  • UI / Layout
  • View Model - The Staging Area
slide-6
SLIDE 6

View Model (JavaScript)

var ViewModel = function(){ this.name = "Daniel Doezema"; this.site = "http://dan.doezema.com"; this.age = 28; }; var view_model = new ViewModel();

slide-7
SLIDE 7

View (HTML)

<h1>Presenter</h1> <h3>Name</h3> <p data-bind="text:name"></p> <h3>Site</h3> <p data-bind="text:site"></p> <h3>Age</h3> <p data-bind="text:age"></p>

  • Bind Attribute
  • Knockout

"Binding"

  • View Model

Property

slide-8
SLIDE 8

Activation / Page Bootstrap

$(function($){ var view_mode = new ViewModel(); ko.applyBindings(view_model); });

slide-9
SLIDE 9

Rendered View

Presenter

Name Daniel Doezema Site http://dan.doezema.com Age 28

slide-10
SLIDE 10

Observables

// Static Properties var ViewModel = function(){ this.name = "Daniel Doezema"; this.site = "http://dan.doezema.com"; this.age = 28; }; // Observable Properties var ViewModel = function(){ this.name = ko.observable("Daniel Doezema"); this.site = ko.observable("http://dan.doezema.com"); this.age = ko.observable(28); };

slide-11
SLIDE 11

Observables

  • Method, acting as property...

view_model.name(); // Getter view_model.name("John Doe"); // Setter

  • Notifies Subscribers
  • Detects Dependencies
  • Reflects Changes of Bindings
slide-12
SLIDE 12

Subscribers

Bi-Directional Bindings

<input data-bind="value:name" /> <input type="checkbox" data-bind="checked:is_admin" />

slide-13
SLIDE 13

Subscribers

Computed Observables

var ViewModel = function(){ var self = this; self.signature = ko.computed(function(){ return self.name() + " - " + self.site(); }); }; <p data-bind="text:signature"></p>

slide-14
SLIDE 14

Observable Arrays

var view_model = new ViewModel(); view_model.links = ko.obserableArray([ {name:"Me", url:"http://dan.doezema.com"}, {name:"Apple", url:"http://apple.com"}, {name:"Twitter", url:"http://twitter.com"} ]); ko.applyBindings(view_model);

slide-15
SLIDE 15

ObservableArray + `foreach` Binding

<ul data-bind="foreach:links"> <li> <a data-bind="attr:{href:$data.url}, text:$data.name"></a> </li> </ul>

  • Me
  • Apple
  • Twitter
slide-16
SLIDE 16

ObservableArray Methods

  • .push
  • .pop
  • .shift
  • .unshift
  • .reverse
  • .sort
  • .splice
  • .slice
  • .remove
  • .removeAll
  • .destroy
  • .destroyAll
slide-17
SLIDE 17

In Action...

http://kojs.dan.doezema.com