Tuesday, July 19, 2016

Knockout JS

What is Knockout JS?

It is a JavaScript library based on MVVM pattern. MVVM help us to build rich and responsive websites. MVVM means model (stored data), view (UI) and view model (it represent the model). This article covers basics of Knockout JS and its concept of program in easy and simple way.

To start with Knockout JS one should have basics of html, CSS, JavaScript, DOM and text editor.
KO is an abbreviation used for KnockoutJS.
Second word for Knockout JS is KO.
KO was developed by Steve Sanderson, employee of Microsoft.
What is Knockout JS?

It is a JavaScript library based on MVVM pattern. MVVM help us to build rich and responsive websites. MVVM means model (stored data), view (UI) and view model (it represent the model). This article covers basics of Knockout JS and its concept of program in easy and simple way.

To start with Knockout JS one should have basics of html, CSS, JavaScript, DOM and text editor.
KO is an abbreviation used for KnockoutJS.
Second word for Knockout JS is KO.
KO was developed by Steve Sanderson, employee of Microsoft.

Why knockout JS?


It is easy to handle data driven interfaces with KO. With KO we can write auto update UI codes. It is cannot replace JQuery but it can work as splement with smart and very useful feature. KO is small & lightweight. KO is independent. It is compatible with both client and server side technologies.


How to use knockout JS?


you can download production build of Knockout.js from its official website



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

Or you can include from CDN:



 <script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js" type="text/javascript"></script>


 Example



 <!DOCTYPE html>
<head>
   <title>KnockoutJS Simple Example</title>
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js" type="text/javascript"></script>
</head>

<body>
   <!-- This is called "view" of HTML markup that defines the appearance of UI -->
   <p>First String: <input data-bind="value: fstring" /></p>
   <p>Second String: <input data-bind="value: sstring" /></p>

   <p>First String: <strong data-bind="text: fstring">Hi</strong></p>
   <p>Second String: <strong data-bind="text: sstring">There</strong></p>

   <p>Derived String: <strong data-bind="text: tstring"></strong></p>

   <script>
   <!-- This is called "viewmodel". This javascript section defines the data and behavior of UI -->

   function AppViewModel() {
       this.fstring = ko.observable("Enter First String");
       this.sstring = ko.observable("Enter Second String");

       this.tstring = ko.computed(function() {
              return this.fstring() + " " + this.sstring();
    }, this);
   }

   // Activates knockout.js
   ko.applyBindings(new AppViewModel());
   </script>
</body>
</html>
  • We have two input boxes : First String and Second String. These 2 variables are initialized with values Enter First String and Enter 
  • Second String respectively in ViewModel. <p>First String: < input data-bind="value: fstring" /> </p>This is how we are binding values from ViewModel to HTML elements using 'data-bind' attribute in the body section.Here 'fstring' refers to ViewModel variable.
  •  this.fstring = ko.observable("Enter First String");ko.observable is a concept which keeps an eye on value changes so that it can update underlying ViewModel data.To understand this better, let's update first input box to "Hello" and Second input box to "world". You will see values are updated simultaneously. We will study more about this concept in KnockoutJS - Observables chapter.
  •  this.tstring = ko.computed(function() {return this.fstring() + " " + this.sstring();}, this); Next we have computed function in viewmodel. This function derives third string based on 2 strings mentioned earlier. So any updates made first to string it directly reflects to the third string. There is no need of writing extra code to accomplish this. This is just a simple example.
Output
Save the above code as example.html. Open this file in your browser and you will see an output as below:





Other interesting things to know?


Features



  • Declarative Binding - DOM elements are directly connected to model using data-bind attribute with very simple syntax.
  • Automatically Refresh for UI- Change made to view model data are reflected in UI automatically and vice-versa. It doesn’t require extra code.
  • Dependency Tracking - Relationships between attributes and library functions/components are transparent.
  • Temp late – it is used to build complex UI structures - with possibility of repeating or nesting blocks - as a function of view model data.
  • Extensible – We can easily extends custom behavior.

KnockoutJS is widely used for Single Page Applications - A web site that is created with ability to retrieve all necessary data dynamically with a single page load reducing server round trips.

KnockoutJS is a client side framework. This is a Javascript library which makes it very easy to bind HTML to domain data. It implements a pattern called"Model-View-ViewModel"(MVVM). Observables is the magic ingredient of KnockoutJS. All data remains in sync because of Observable attribute.


View


View is nothing but User Interface created using HTML elements and CSS styling.

You can bind HTML DOM elements to data model using KnockoutJS. It provides 2 way data binding between View and ViewModel using 'data-bind' concept, which means any updates done in UI are reflected in data model and any changes done in data model are reflected in UI. One can create self-updating UI with help ofknockoutJS.

ViewModel


ViewModel is a Javascript object which contains necessary properties and functions to represent data. View and ViewModel are connected together with declarative data-bind concept used in HTML. This makes it easy to change HTML without changing ViewModel. KnockoutJS takes care of automatic data refresh between them through use of Observables.

Synchronization of data is achieved through binding DOM elements to Data Model first using data-bind and then refreshing these 2 components through use of Observables. Dependency tracking is done automatically due to this synchronization of data. No extra coding is required to achieve it. KnockoutJS allows you to create direct connection between your display and underlying data.
You can create your own bindings called as custom bindings for application specific behaviors. This way knockout gives direct control of how you want to transform your data into HTML.

Model


Model is domain data on server and it gets manipulated as and when request is sent/received from ViewModel.

The data could be stored in database, cookie or other form of persistent storage. KnockoutJS does not worry about how it is stored. It is up to programmer to communicate between stored data and KnockoutJS.


Why knockout JS?


It is easy to handle data driven interfaces with KO. With KO we can write auto update UI codes. It is cannot replace JQuery but it can work as splement with smart and very useful feature. KO is small & lightweight. KO is independent. It is compatible with both client and server side technologies.


How to use knockout JS?


you can download production build of Knockout.js from its official website



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

Or you can include from CDN:



 <script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js" type="text/javascript"></script>


 Example



 <!DOCTYPE html>
<head>
   <title>KnockoutJS Simple Example</title>
   <script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js" type="text/javascript"></script>
</head>

<body>
   <!-- This is called "view" of HTML markup that defines the appearance of UI -->
   <p>First String: <input data-bind="value: fstring" /></p>
   <p>Second String: <input data-bind="value: sstring" /></p>

   <p>First String: <strong data-bind="text: fstring">Hi</strong></p>
   <p>Second String: <strong data-bind="text: sstring">There</strong></p>

   <p>Derived String: <strong data-bind="text: tstring"></strong></p>

   <script>
   <!-- This is called "viewmodel". This javascript section defines the data and behavior of UI -->

   function AppViewModel() {
       this.fstring = ko.observable("Enter First String");
       this.sstring = ko.observable("Enter Second String");

       this.tstring = ko.computed(function() {
              return this.fstring() + " " + this.sstring();
    }, this);
   }

   // Activates knockout.js
   ko.applyBindings(new AppViewModel());
   </script>
</body>
</html>

  • We have two input boxes : First String and Second String. These 2 variables are initialized with values Enter First String and Enter Second String respectively in ViewModel.
  •  <p>First String: < input data-bind="value: fstring" /> </p>This is how we are binding values from ViewModel to HTML elements using 'data-bind' attribute in the body section.Here 'fstring' refers to ViewModel variable.
  •  this.fstring = ko.observable("Enter First String");ko.observable is a concept which keeps an eye on value changes so that it can update underlying ViewModel data.To understand this better, let's update first input box to "Hello" and Second input box to "world". You will see values are updated simultaneously. We will study more about this concept in KnockoutJS - Observables chapter.
  • this.tstring = ko.computed(function() {return this.fstring() + " " + this.sstring();}, this); Next we have computed function in viewmodel. This function derives third string based on 2 strings mentioned earlier. So any updates made first to string it directly reflects to the third string. There is no need of writing extra code to accomplish this. This is just a simple example.

Output
Save the above code as example.html. Open this file in your browser and you will see an output as below:





Other interesting things to know?


Features


  • Declarative Binding - DOM elements are directly connected to model using data-bind attribute with very simple syntax.
  • Automatically Refresh for UI- Change made to view model data are reflected in UI automatically and vice-versa. It doesn’t require extra code.
  • Dependency Tracking - Relationships between attributes and library functions/components are transparent.
  • Temp late – it is used to build complex UI structures - with possibility of repeating or nesting blocks - as a function of view model data.
  • Extensible – We can easily extends custom behavior.

KnockoutJS is widely used for Single Page Applications - A web site that is created with ability to retrieve all necessary data dynamically with a single page load reducing server round trips.


KnockoutJS is a client side framework. This is a Javascript library which makes it very easy to bind HTML to domain data. It implements a pattern called"Model-View-ViewModel"(MVVM). Observables is the magic ingredient of KnockoutJS. All data remains in sync because of Observable attribute.


View


View is nothing but User Interface created using HTML elements and CSS styling.

You can bind HTML DOM elements to data model using KnockoutJS. It provides 2 way data binding between View and ViewModel using 'data-bind' concept, which means any updates done in UI are reflected in data model and any changes done in data model are reflected in UI. One can create self-updating UI with help ofknockoutJS.

ViewModel


ViewModel is a Javascript object which contains necessary properties and functions to represent data. View and ViewModel are connected together with declarative data-bind concept used in HTML. This makes it easy to change HTML without changing ViewModel. KnockoutJS takes care of automatic data refresh between them through use of Observables.

Synchronization of data is achieved through binding DOM elements to Data Model first using data-bind and then refreshing these 2 components through use of Observables. Dependency tracking is done automatically due to this synchronization of data. No extra coding is required to achieve it. KnockoutJS allows you to create direct connection between your display and underlying data.
You can create your own bindings called as custom bindings for application specific behaviors. This way knockout gives direct control of how you want to transform your data into HTML.

Model


Model is domain data on server and it gets manipulated as and when request is sent/received from ViewModel.

The data could be stored in database, cookie or other form of persistent storage. KnockoutJS does not worry about how it is stored. It is up to programmer to communicate between stored data and KnockoutJS.

Post a Comment