SlideShare une entreprise Scribd logo
1  sur  50
Télécharger pour lire hors ligne
INTRODUCTION
   Knockout is a JavaScript library that
helps you to create rich, responsive
display and editor user interfaces with
a clean underlying data model.
HEADLINE FEATURES

Declarative Binding
Automatic UI Refresh
Dependency Tracking
Templating
ADDITIONAL FEATURES

• Pure JavaScript Library
• Compact (around 13kb after zipping)
• Works on any mainstream browser
  (IE 6+, Firefox 2+, Chrome, Safari, others)
SOME QUESTIONS
1. Is KO intended to compete with jQuery (or
   Prototype, etc.) or work with it?
2. How is Knockout different?
TO SUMMARIES
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.
HOW TO USE
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>


                     Installation
KEY KO CONCEPTS
MVVM & View Model
Observables
Bindings
Templates and Customization
MVVM (MODEL-VIEW-VIEW MODEL)
      MODEL-VIEW-
VIEW MODELS
 To create a view model
with KO, just declare any
JavaScript object. For
example,

var myViewModel = {
   personName: 'Bob',
   personAge: 123
};
USING VIEWMODEL INSIDE A VIEW
 Example to create a very simple view of
 that view model using a declarative
 binding :
The name is <span data-bind="text:
personName"></span>
ACTIVATING KNOCKOUT.JS

ko.applyBindings(myViewModel);




     The name is <span>Bob</span>
OBSERVABLES

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.
DECLARING OBSERVABLE PROPERTIES

var myViewModel = {
   personName: ko.observable('Bob'),
   personAge: ko.observable(123)
};
R/W OBSERVABLE PROPERTIES
  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.
COMPUTED OBSERVABLES
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 OBSERVABLE – DECOMPOSING USER INPUT
COMPUTED OBSERVABLE – A VALUE CONVERTER
COMPUTED OBSERVABLE – FILTERING AND VALIDATING USER INPUT
DETERMINING IF A PROPERTY IS A COMPUTED
              OBSERVABLE
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 observable Array.
  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.
READING INFORMATION FROM AN OBSERVABLE ARRAY

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




 FUNCTIONS FOR OBSERVABLE COLLECTION
    indexOf, slice, pop, pus, unshift, reverse, sort, splice,
  remove, removeAll, destroy, destroyAll.
KNOCKOUT BUILT IN BINDINGS
CONTROLLING TEXT AND APPEARANCE


The visible binding
The text binding
The html binding
The css binding
The style binding
The attr binding
THE VISIBLE BINDING
THE TEXT BINDING
THE HTML BINDING
THE CSS BINDING
THE STYLE BINDING
THE ATTR BINDING
CONTROLLING FLOW
THE FOREACH BINDING
THE IF BINDING
THE IFNOT BINDING
THE WITH BINDING
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
THE CLICK BINDING
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>
THE EVENT BINDING
THE SUBMIT BINDING
THE ENABLE BINDING
THE DISABLE BINDING


This is the mirror image of the enable binding.
THE VALUE BINDING
THE HASFOCUS BINDING
THE CHECKED BINDING
THE OPTIONS BINDING
THE SELECTED OPTIONS BINDING
THE UNIQUENAME BINDING
Thanks & Regards
   Narendra Kumar P
Eminosoft India Pvt.Ltd.

Contenu connexe

Tendances

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...FalafelSoftware
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directivesAlexe Bogdan
 
Jquery tutorial-beginners
Jquery tutorial-beginnersJquery tutorial-beginners
Jquery tutorial-beginnersIsfand yar Khan
 
Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJSBrajesh Yadav
 
Angular Mini-Challenges
Angular Mini-ChallengesAngular Mini-Challenges
Angular Mini-ChallengesJose Mendez
 
JavaScript: DOM and jQuery
JavaScript: DOM and jQueryJavaScript: DOM and jQuery
JavaScript: DOM and jQuery維佋 唐
 
AngularJS custom directive
AngularJS custom directiveAngularJS custom directive
AngularJS custom directiveNascenia IT
 
Developing large scale JavaScript applications
Developing large scale JavaScript applicationsDeveloping large scale JavaScript applications
Developing large scale JavaScript applicationsMilan Korsos
 
Intro to Angular.JS Directives
Intro to Angular.JS DirectivesIntro to Angular.JS Directives
Intro to Angular.JS DirectivesChristian Lilley
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsEPAM Systems
 
Custom AngularJS Directives
Custom AngularJS DirectivesCustom AngularJS Directives
Custom AngularJS Directivesyprodev
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developersKai Koenig
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseEPAM Systems
 

Tendances (20)

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directives
 
Jquery tutorial-beginners
Jquery tutorial-beginnersJquery tutorial-beginners
Jquery tutorial-beginners
 
Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJS
 
Angular Mini-Challenges
Angular Mini-ChallengesAngular Mini-Challenges
Angular Mini-Challenges
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
JavaScript: DOM and jQuery
JavaScript: DOM and jQueryJavaScript: DOM and jQuery
JavaScript: DOM and jQuery
 
reveal.js 3.0.0
reveal.js 3.0.0reveal.js 3.0.0
reveal.js 3.0.0
 
AngularJS custom directive
AngularJS custom directiveAngularJS custom directive
AngularJS custom directive
 
Developing large scale JavaScript applications
Developing large scale JavaScript applicationsDeveloping large scale JavaScript applications
Developing large scale JavaScript applications
 
Intro to Angular.JS Directives
Intro to Angular.JS DirectivesIntro to Angular.JS Directives
Intro to Angular.JS Directives
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
 
Custom AngularJS Directives
Custom AngularJS DirectivesCustom AngularJS Directives
Custom AngularJS Directives
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developers
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
Web components
Web componentsWeb components
Web components
 

Similaire à Knockoutjs

Knockoutjs databinding
Knockoutjs databindingKnockoutjs databinding
Knockoutjs databindingBoulos Dib
 
KnockOutjs from Scratch
KnockOutjs from ScratchKnockOutjs from Scratch
KnockOutjs from ScratchUdaya Kumar
 
The Magic of WPF & MVVM
The Magic of WPF & MVVMThe Magic of WPF & MVVM
The Magic of WPF & MVVMAbhishek Sur
 
Design Patterns in ZK: Java MVVM as Model-View-Binder
Design Patterns in ZK: Java MVVM as Model-View-BinderDesign Patterns in ZK: Java MVVM as Model-View-Binder
Design Patterns in ZK: Java MVVM as Model-View-BinderSimon Massey
 
Introduction to Knockout Js
Introduction to Knockout JsIntroduction to Knockout Js
Introduction to Knockout JsKnoldus Inc.
 
Bringing the light to the client with KnockoutJS
Bringing the light to the client with KnockoutJSBringing the light to the client with KnockoutJS
Bringing the light to the client with KnockoutJSBoyan Mihaylov
 
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
 
MVVM & Data Binding Library
MVVM & Data Binding Library MVVM & Data Binding Library
MVVM & Data Binding Library 10Clouds
 
Creating lightweight JS Apps w/ Web Components and lit-html
Creating lightweight JS Apps w/ Web Components and lit-htmlCreating lightweight JS Apps w/ Web Components and lit-html
Creating lightweight JS Apps w/ Web Components and lit-htmlIlia Idakiev
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular jsAayush Shrestha
 
Web Components Everywhere
Web Components EverywhereWeb Components Everywhere
Web Components EverywhereIlia Idakiev
 

Similaire à Knockoutjs (20)

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
 
Knockoutjs
KnockoutjsKnockoutjs
Knockoutjs
 
Knockout.js
Knockout.jsKnockout.js
Knockout.js
 
KnockOutjs from Scratch
KnockOutjs from ScratchKnockOutjs from Scratch
KnockOutjs from Scratch
 
Knockout in action
Knockout in actionKnockout in action
Knockout in action
 
Knockout js session
Knockout js sessionKnockout js session
Knockout js session
 
XAML/C# to HTML5/JS
XAML/C#  to HTML5/JS XAML/C#  to HTML5/JS
XAML/C# to HTML5/JS
 
The Magic of WPF & MVVM
The Magic of WPF & MVVMThe Magic of WPF & MVVM
The Magic of WPF & MVVM
 
Design Patterns in ZK: Java MVVM as Model-View-Binder
Design Patterns in ZK: Java MVVM as Model-View-BinderDesign Patterns in ZK: Java MVVM as Model-View-Binder
Design Patterns in ZK: Java MVVM as Model-View-Binder
 
Introduction to Knockout Js
Introduction to Knockout JsIntroduction to Knockout Js
Introduction to Knockout Js
 
Bringing the light to the client with KnockoutJS
Bringing the light to the client with KnockoutJSBringing the light to the client with KnockoutJS
Bringing the light to the client with KnockoutJS
 
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!
 
MVVM & Data Binding Library
MVVM & Data Binding Library MVVM & Data Binding Library
MVVM & Data Binding Library
 
Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
 
Creating lightweight JS Apps w/ Web Components and lit-html
Creating lightweight JS Apps w/ Web Components and lit-htmlCreating lightweight JS Apps w/ Web Components and lit-html
Creating lightweight JS Apps w/ Web Components and lit-html
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
Web Components Everywhere
Web Components EverywhereWeb Components Everywhere
Web Components Everywhere
 
Backbone js
Backbone jsBackbone js
Backbone js
 

Knockoutjs