knockout.js explained

49

Upload: paxcel-technologies

Post on 22-May-2015

2.548 views

Category:

Technology


0 download

DESCRIPTION

A comprehensive explanation and analysis of knockout.js with examples

TRANSCRIPT

Page 1: Knockout.js explained
Page 2: Knockout.js explained

Introduction

Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model.

Any time you have sections of UI that update dynamically (e.g., changes depending on the user’s actions or when an external data source changes), KO can help you implement it in a simple yet robust way.

Page 3: Knockout.js explained

Headline Features

• Declarative Binding• Automatic UI Refesh• Dependency Tracking• Templating

Page 4: Knockout.js explained

Additional Features

• Pure Javascript Library• Compact (around 13kb after gzipping)• Works on any mainstream browser (IE 6+, Firefox 2+, Chrome, Safari, others)

Page 5: Knockout.js explained

Some Questions

1. Is KO intended to compete with jQuery (or Prototype, etc.) or work with it?

2. How is Knockout different?

Page 6: Knockout.js explained

To Summarise

KO doesn’t compete with jQuery or similar low-level DOM APIs. KO provides a complementary, high-level way to link a data model to a UI. KO itself doesn’t depend on jQuery, but you can certainly use jQuery at the same time, and indeed that’s often useful if you want things like animated transitions.

Page 7: Knockout.js explained

• Download the latest version of the Knockout JavaScript file

• Reference the file using a <script> tag somewhere on your HTML pages

<script type='text/javascript' src='knockout-2.1.0.js'></script>

How To Use

Page 8: Knockout.js explained

• MVVM & View Model• Observables• Bindings• Templates and Customization

Key KO Concepts

Page 9: Knockout.js explained

To create a view model with KO, just declare any JavaScript object. For example,

var myViewModel = { personName: 'Bob', personAge: 123};

View Models

Page 10: Knockout.js explained

Example to create a very simple view of that view model using a declarative binding :

The name is <span data-bind="text: personName"></span>

Using ViewModel inside a View

Page 11: Knockout.js explained

ko.applyBindings(myViewModel); That does it! Now, view will display as if you’d written the

following HTML:

The name is <span>Bob</span>

Activating knockout.js

Page 12: Knockout.js explained

Question: How can KO know when parts of your view model change?

Answer: You need to declare your model properties as observables, because these are special JavaScript objects that can notify subscribers about changes, and can automatically detect dependencies.

Observables

Page 13: Knockout.js explained

var myViewModel = { personName: ko.observable('Bob'), personAge: ko.observable(123)};

Declaring Observable Properties

Page 14: Knockout.js explained

• To read myViewModel.personName()• To write myViewModel.personName('Mary') • To write values to multiple observable properties

myViewModel.personName(‘PN1').personAge(50)

So, when we write data-bind="text: personName", the text binding registers itself to be notified when personName changes (assuming it’s an observablevalue, which it is now).

R/W Observable Properties

Page 15: Knockout.js explained

• .subscribe() function• .dispose() function

Explicitly Subscribing to observables

Page 16: Knockout.js explained

These are functions that are dependent on one or more other observables, and will automatically update whenever any of these dependencies change.

function AppViewModel() { this.firstName = ko.observable('Bob'); this.lastName = ko.observable('Smith'); this.fullName = ko.computed(function() { return this.firstName() + " " + this.lastName(); }, this);}

Computed Observables

Page 17: Knockout.js explained

Computed Observable – Decomposing User Input

Page 18: Knockout.js explained

Computed Observable – A value converter

Page 19: Knockout.js explained

Computed Observable – Filtering and validating user input

Page 20: Knockout.js explained

Determining if a property is a computed observable

Page 21: Knockout.js explained

Observable Arrays

If you want to detect and respond to changes on one object, you’d use observables. If you want to detect and respond to changes of a collection of things, use an observableArray. This is useful in many scenarios where you’re displaying or editing multiple values and need repeated sections of UI to appear and disappear as items are added and removed.

Page 22: Knockout.js explained

Reading Information from an Observable Array

// This observable array initially contains three objectsvar anotherObservableArray = ko.observableArray([ { name: "Bungle", type: "Bear" }, { name: "George", type: "Hippo" }, { name: "Zippy", type: "Unknown" }]);

Functions for Observable Collection indexOf, slice, pop, push, shift, unshift, reverse, sort, splice, remove,

removeAll, destroy, destroyAll

Page 23: Knockout.js explained

Knockout built in Bindings -

1. Controlling text and appearance2. Control Flow3. Working with Form Fields

Page 24: Knockout.js explained

Controlling text and appearance

• The visible binding• The text binding• The html binding• The css binding• The style binding• The attr binding

Page 25: Knockout.js explained

The visible binding

Page 26: Knockout.js explained

The text binding

Page 27: Knockout.js explained

The html binding

Page 28: Knockout.js explained

The css binding

Page 29: Knockout.js explained

The style binding

Page 30: Knockout.js explained

The attr binding

Page 31: Knockout.js explained

Controlling Flow

• The foreach binding• The if binding• The ifnot binding• The with binding

Page 32: Knockout.js explained

The foreach binding

Page 33: Knockout.js explained

The if binding

Page 34: Knockout.js explained

The ifnot binding

Page 35: Knockout.js explained

The with binding

Page 36: Knockout.js explained

Working with Form Fields

• The click binding• The event binding• The submit binding• The enable binding• The disable binding• The value binding• The hasfocus binding• The checked binding• The options binding• The selectedOptions binding• The uniqueName binding

Page 37: Knockout.js explained

The click binding

Page 38: Knockout.js explained

Allowing the default click action

• By default, Knockout will prevent the click event from taking any default action.

• However, if you do want to let the default click action proceed, just return true from your click handler function.

Preventing the event from bubbling

<div data-bind="click: myDivHandler"> <button data-bind="click: myButtonHandler, clickBubble: false"> Click me </button></div>

Page 39: Knockout.js explained

The event binding

Page 40: Knockout.js explained

The submit binding

Page 41: Knockout.js explained

The enable binding

Page 42: Knockout.js explained

The disable binding

This is the mirror image of the enable binding.

Page 43: Knockout.js explained

The value binding

Page 44: Knockout.js explained

The hasFocus binding

Page 45: Knockout.js explained

The checked binding

Page 46: Knockout.js explained

The options binding

Page 47: Knockout.js explained

The selectedoptions binding

Page 48: Knockout.js explained

The uniqueName binding

Page 49: Knockout.js explained

Thanks, Sumit Rathee & Mohit Kumar [email protected] [email protected] Website: www.paxcel.net

* The content of this presentation is derived from knockout official website documentation and samples: http://knockoutjs.com/