What is KnockOut.js
Knockout.js is a JavaScript library that provides two-way data binding between our user interfaces and data model. Means we can dynamically bind our data i.e. any time we can update our UI. By using Knockout.js it is very easy & simple. There are other features also we will learn step by step.
Features of Knockout.js
- Automatic Refresh.
- Dependency Tracking - If our data model gets change then it automatically updates in UI.
- Declarative Binding - We can bind data to DOM using data-bind attribute to DOM elements.
- Templating - No need to show any internal structure.
- Trivially Extensible - Reuse of code is very easy in minimum lines of code.
Common Benefits
- It is purely based on JavaScript library.
- No need to require any architecture changes i.e. we can work on web applications using UI part.
- Compatible with all browsers.
Step 1: Create simple project with empty MVC application using VS 2013.
Step 2: Click on add KnockOut.js & JavaScript files from Manage Nuget packages.
Step 3:
Now add one JavaScript file for JavaScript & Knockout.js code.
Also add references to above created file as follows,
- /// <reference path="jquery-2.2.0.js" />
- /// <reference path="knockout-3.4.0.js" />
You can drag drop this above two files on top of our new JavaScript file,
Step 4:
Now create one controller & write any action method in that controller. Now create one View for above action method & write the following code in View.
- <head>
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Scripts/knockout-3.4.0.js"></script>
- <script src="~/Scripts/Before-KO.js"></script>
- </head>
- <body>
- <h2>Using Knockout.js</h2>
First Name:
- <input type="text" data-bind="value:myFirstName" />
- <br />
Middle Name:
- <input type="text" data-bind="value:myMiddleName" />
- <br/>
Last Name:
- <input type="text" data-bind="value:myLastName" />
- <br/>
Output :
- <span data-bind="text:finalOutput"></span>
- </body>
Step 5:
Write the following code to our JavaScript files,
- /// <reference path="jquery-2.2.0.js" />
- /// <reference path="knockout-3.4.0.js" />
- $(document).ready(function()
- {
- //// code using KnockOut.js
- var myDetailViewModel =
- {
- myFirstName: ko.observable("Rupeshkumar"),
- myMiddleName: ko.observable("J..."),
- myLastName: ko.observable("Kahane"),
- };
- myDetailViewModel.finalOutput = ko.computed(function()
- {
- var x = this.myFirstName() + " " + this.myMiddleName() + " " + this.myLastName();
- return x;
- },
- myDetailViewModel);
- ko.applyBindings(myDetailViewModel);
- })
Step 6:
Suppose we change values in our textbox then our output will be like the following:
ViewModel
- var myDetailViewModel =
- {
- myFirstName: ko.observable("Rupeshkumar"),
- myMiddleName: ko.observable("J..."),
- myLastName: ko.observable("Kahane"),
- };
In above code myDetailViewModel is ViewModel name & Observable is used to bind a data & if there are any changes in data from UI then it will notify automatically.
This data is bind to DOM using data-bind attribute to View as follows,
- <input type="text" data-bind="value:myFirstName" />
- <input type="text" data-bind="value:myMiddleName" />
- <input type="text" data-bind="value:myLastName" />
Computed observable function
- myDetailViewModel.finalOutput = ko.computed(function()
- {
- var x = this.myFirstName() + " " + this.myMiddleName() + " " + this.myLastName();
- return x;
- },
- myDetailViewModel);
- ko.applyBindings(myDetailViewModel);
Computed observable function takes three parameters as function name, target ViewModel, options.
To show output in UI i.e. bind data to view is same as above.
- <span data-bind="text:finalOutput"></span>
Summary:
This article helps fresher candidates to understand KnockOut.Js.


Bhuvanesh MohankumarPosted May 3, 2016, 8:40 AM EDT
Thanks
Raushan SinhaPosted Mar 28, 2016, 1:23 AM EDT
very clear explanation dude
Mohsin AzamPosted Feb 18, 2016, 4:44 AM EST
nice one
Rupali ShindePosted Feb 7, 2016, 6:32 PM EST
Thank you for this example. as i am new to knowkout.js, it is better if you write one article on what is basic architecture of it. how calling happen from use from UI to js function? what KO stands for in this article ??
Rupesh KahanePosted Feb 6, 2016, 8:00 AM EST
Manas Mohapatra Sure. I will upload soon. You can refer my another KnockOut.js Part 2 article with source code @http://www.c-sharpcorner.com/UploadFile/a8024d/introduction-to-knockout-js-part-2/
Rupesh KahanePosted Feb 6, 2016, 7:58 AM EST
thanks to Santhakumar Munuswamy, Shubham Kumar & Shashangka Shekhar also
Shashangka ShekharPosted Feb 6, 2016, 6:22 AM EST
Thanks for sharing
Manas MohapatraPosted Feb 6, 2016, 2:55 AM EST
Good start on Knockout. It would be better if you can add sample project with the article.
Shubham KumarPosted Feb 5, 2016, 11:30 PM EST
nice
Santhakumar MunuswamyPosted Feb 5, 2016, 10:40 AM EST
Thanks for nice article
Rupesh KahanePosted Feb 5, 2016, 9:02 AM EST
thanks Debasis Saha
Debasis SahaTOP 100Posted Feb 4, 2016, 10:35 PM EST
Good One..