Before reading this article, I highly recommend reading the previous part of the series on Knockout.JS,
- Introduction To Knockout.JS - Part 1
- Introduction To Knockout.JS - Part 2
- Introduction To Knockout.JS - Part 3
Now we are going to understand binding contexts with special properties and control flow statements.
- $Data: View Model object in the current context
- $Index: Zero based Index of the current array entry being rendered by a foreach binding. It is automatically updated fromthe observable array.
- $ParentContext: Binding context at the parent level.
Control Flow Statement
Foreach:
Step 1: Create one project in VS 2013. I am going to create MVC empty application.
- public ActionResult Array()
- {
- return View();
- }
Step 2: The foreach binding adds a markup for each entry in an array, and binds each copy of that markup to the corresponding array item and shows that values using data-bind. Array elements are automatically print using foreach.
Step 3: I have created view as follows which contains the example of binding view model objects using $Data in current context.
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Example of foreach using $Data</title>
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Scripts/knockout-3.4.0.js"></script>
- <script>
- $(document).ready(function () {
- function salaryViewModel() {
- var empSalary = this;
- empSalary.items = ['25000', '20000', '150000'];
- }
- ko.applyBindings(new salaryViewModel());
- })
- </script>
- </head>
- <body>
- <div>
- <h4 style="color:brown">Example of foreach using $Data</h4>
- <ul data-bind="foreach: items">
- <li>The value is
- <span data-bind="text: $data"></span>
- </li>
- </ul>
- </div>
- </body>
- </html>

Example of Binding context using $index properties
Step 1: I am going to create another action method and create a new view for the same action method.
- public ActionResult UsingIndexProperty()
- {
- return View();
- }
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Binding Context using $index</title>
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Scripts/knockout-3.4.0.js"></script>
- <script>
- $(document).ready(function () {
- function DataViewModel() {
- this.myNumbers = ko.observable(5);
- }
- ko.applyBindings(new DataViewModel());
- })
- </script>
- </head>
- <body>
- <div>
- <h4 style="color:darkmagenta">Example of Binding Context using $index Property</h4>
- <ul data-bind="foreach: new Array(myNumbers())">
- <li data-bind='text: $index()+1000'></li>
- </ul>
- </div>
- </body>
- </html>

If:
If binding causes a section of markup to appear in View using data-bind whenever if condition evaluates to true if condition is same as visible binding (we had discussed in my Introduction To Knockout.JS - Part 2).
Step 1: I am going to create another action method & create new view for the same action method.
- public ActionResult if_Condition()
- {
- return View();
- }
Step 2: Add the following code to our view:
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>if condition</title>
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Scripts/knockout-3.4.0.js"></script>
- <script>
- $(document).ready(function () {
- ko.applyBindings({
- myValue: ko.observable(false)
- });
- });
- </script>
- </head>
- <body>
- <h4>Example of If condition</h4>
- <button data-bind="click:myValue"> My Value </button>
- <div data-bind="if: myValue">
- <br />Welcome to if condition.
- </div>
- </body>
- </html>

Whenever we click on button if condition evaluates to TRUE and this will show text message.
Ifnot:
If not, binding causes a section of markup to disappear in View using data-bind of if not condition.
Step 1: I am going to create another action method & creating new view for the same action method.
- public ActionResult Not_if()
- {
- return View();
- }
Step 2: Add the following code to our view:
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>ifnot condition</title>
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Scripts/knockout-3.4.0.js"></script>
- <script>
- $(document).ready(function () {
- ko.applyBindings({
- myValue: ko.observable(false)
- });
- });
- </script>
- </head>
- <body>
- <h4>Example of Ifnot condition</h4>
- <label>
- <input type="checkbox" data-bind="checked: myValue" /> click in checkbox to hide bellow text
- </label>
- <div data-bind="ifnot: myValue">
- <br /> Welcome to ifnot condition.
- </div>
- </body>
- </html>

Whenever we click checkbox, the text will disappear as:
Another example of foreach condition
Step 1: Create another action & view for same action method.
- public ActionResult ForeachLoop()
- {
- return View();
- }
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Initialize an Array </title>
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Scripts/knockout-3.4.0.js"></script>
- <script>
- $(document).ready(function () {
- ko.applyBindings({
- bookDetails: [
- { bookName: 'WCF', author: 'Vithal W.' },
- { bookName: 'AngularJs', author: 'Jeetendra G.' },
- { bookName: 'MVC', author: 'Rupesh K.' }
- ]
- });
- })
- </script>
- </head>
- <body>
- <div>
- <h4 style="color:deepskyblue">Foreach loop example</h4>
- <table>
- <thead>
- <tr style="color:red">
- <th>Book Name</th>
- <th>Author Name</th>
- </tr>
- </thead>
- <tbody data-bind="foreach: bookDetails">
- <tr>
- <td data-bind="text: bookName"></td>
- <td data-bind="text: author"></td>
- </tr>
- </tbody>
- </table>
- </div>
- </body>
- </html>
Now run our application and see the output:

Summary
This article will help fresher candidates to understand KnockOut.Js.
Read more articles on Knockout.JS:

Bhuvanesh MohankumarPosted May 3, 2016, 12:39 PM
Great article
Ehsan SajjadPosted Feb 18, 2016, 1:18 PM
Very good article
Sibeesh VenuPosted Feb 18, 2016, 12:12 AM
Nice Share
Santhakumar MunuswamyPosted Feb 16, 2016, 2:50 PM
Thanks for nice article
Debasis SahaPosted Feb 16, 2016, 12:30 AM
Nice one...
sreenivasa kPosted Feb 15, 2016, 12:06 PM
very good
Prasham SabadraPosted Feb 15, 2016, 11:28 AM
Thanks Rupesh for sharing this!
Sr KarthigaPosted Feb 15, 2016, 7:21 AM
Good one sir
Kumaresh RajalingamPosted Feb 15, 2016, 7:06 AM
Nice share
Rupesh KahanePosted Feb 15, 2016, 4:47 AM
Thanks a lot
Nanddeep NachanPosted Feb 15, 2016, 4:28 AM
Nice share