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:

Bhuvanesh MohankumarPosted May 3, 2016, 12:40 PM
Thanks
Raushan SinhaPosted Mar 28, 2016, 5:23 AM
very clear explanation dude
Mohsin AzamPosted Feb 18, 2016, 9:44 AM
nice one
Rupali ShindePosted Feb 7, 2016, 11:32 PM
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, 1:00 PM
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, 12:58 PM
thanks to Santhakumar Munuswamy, Shubham Kumar & Shashangka Shekhar also
Shashangka ShekharPosted Feb 6, 2016, 11:22 AM
Thanks for sharing
Manas MohapatraPosted Feb 6, 2016, 7:55 AM
Good start on Knockout. It would be better if you can add sample project with the article.
Shubham KumarPosted Feb 6, 2016, 4:30 AM
nice
Santhakumar MunuswamyPosted Feb 5, 2016, 3:40 PM
Thanks for nice article
Rupesh KahanePosted Feb 5, 2016, 2:02 PM
thanks Debasis Saha
Debasis SahaPosted Feb 5, 2016, 3:35 AM
Good One..