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,
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,
-
-
- $(document).ready(function()
- {
-
- 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:
Now run application,
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.