Working with Knockout.js Part 6: Working with text binding

Working with Knockout.js Part 5: Working with visible binding

Working with Knockout.js Part 4: Observable Arrays

Working with Knockout.js Part 3: Working with Observables

Working with Knockout.js part 2: Understanding MVVM

Working with Knockout.js Part 1: Getting started with Knockoutjs

In last post we started learning about bindings. We had a look on visible binding . In this post we will learn a very important text binding.

Text binding is used to set text property of view elements from the properties or values of ViewModel. You can bind text of a span as easily as depicted in following code snippet,


<script type="text/javascript">

<em id="__mceDel">
 var studentViewModel = {
 studentName: ko.observable('DJ'),

 }

 </script>
<body>
 <span data-bind="text:studentName"></span>
 <script>
 ko.applyBindings(studentViewModel);
 </script>
</body></em>

Above we are creating a ViewModel named studentViewModel with property studentName. In HTML we are binding text property of span. So we can bind text property using following syntax,

Capture

Some time you may come across requirement to dynamically set the value of text binding. That is very much possible using functions and expressions to determine value of the text. Let us assume we have a function in ViewModel as give below,


var studentViewModel = {
 marks: ko.observable(90),
 }

And we can create computed function as following. This function will determine grade of the student.

 studentViewModel.grade = ko.computed(function () {

 if (this.marks > 96)
 return "Grade A";
 else
 return "Grade B";

 }, studentViewModel);

And we can simply bind computed property as text binding to span element on view. You can do this as following,


<body>
 <span data-bind="text:grade"></span>
 <script>
 ko.applyBindings(studentViewModel);
 </script>
</body>

The other way we can dynamically set text binding value by using conditional statement while binding. We can do bind grade without computed function as following


<span data-bind="text:marks()> 10 ? 'Grade A':'Grade B'"></span>

In this way we can work with text binding in knockoutjs. In further posts we will discuss other bindings. I hope you find this post useful. Thanks for reading.

Thanks Julia Lerman for wonderful gift

This is one of the most excited day of my life. I got a gift from one of my ever inspiration Julia Lerman. She sent book authored by her with her autograph on that. I am simply happy. I thank Julie and Rowan for their love. This is surely a motivation for me.

clip_image002

You can find more details about book on Amazon

clip_image003

There are 9 chapters in the book and I cannot wait to start learning it. Buy this book from Flipkart here

Once again thank you so much Julie and Rowan for the impeccable gift. Last but not least I would extend thank to my friend Amit and Mahinder for bringing it for me all from USA.

Happy Coding guys !

What is Method Invocation Pattern in JavaScript

In this post we will take a look on “Method Invocation Pattern” in JavaScript. In JavaScript when function is defined as property of object then it is known as Method

image

Assume we have a JavaScript object as following. In studentObject object property findgrade is a function. So we can say findgrade is a method.


var studentObject =
{
name: "dj",
marks: 89,
findgrade: function (marks) {
if (marks > 75) {
return "Grade A ";
}
else {
return "Grade B ";
}
}
}

Invocation of method is known as “Method Invocation Pattern”. We can invoke method using dot operator on object. Method can be invoked as following,


var grade = studentObject.findgrade(99);
alert(grade);

A Method can access its parent object using operator this. Let us go ahead and modify studentobject as following. Now you can see that we are accessing other properties of object in method using this operator.


var studentObject =
{
name: "dj",
marks: 89,
grade : null,
findgrade: function (marks) {
if (marks > 75) {
this.grade = "A";
}
else {
this.grade = "B";
}
}
}

Now we can set grade property with invocation of findgrade method.


studentObject.findgrade(99);
var grd = studentObject.grade;
alert(grd);

Binding of method to object happens when we execute the method. In this way we can work with Methods and Method Invocation Pattern in JavaScript. I hope you find this post useful. Thanks for reading.

Speaking on Access Data in .Net: Understating different ORM and Services

Hello readers I am excited to inform you that on this Saturday 23rd Feb 2013 I am presenting on Access Data in .Net for Mindcrcaker Chapter Meet event Pizza, Tea and Code

Register and Find full details of event here

If you are in Noida and want to learn from experts with slice of Pizza then you may want to join in. This is a free event and any one can join in.

I will focus on demo oriented session and do demos on

  • Working with LINQ
  • Working with ADO.Net Entity Model
  • Working with Telerik Open Access ORM
  • Comparisons of different ORM
  • Working with WCF Data Service
  • Creating Data Services with Telerik Open Access ORM
  • Working with SQL Azure data

Event will be held on in Sector 63 Noida in Mindcrcaker Office.

Register for the event here

Find full information about event here

I hope to see you there on Saturday.

How to find index of an item in JavaScript Object Array?

Recently while working I came across a scenario. I had to find index of a particular item on given condition from a JavaScript object array. In this post we will see how to find index of object from JavaScript array of object.

Let us assume we have a JavaScript array as following,


var studentsArray =
[
{
"rollnumber": 1,
"name": "dj",
"subject": "physics"
},
{
"rollnumber": 2,
"name": "tanmay",
"subject": "biology"
},
{
"rollnumber": 3,
"name": "amit",
"subject": "chemistry"
},
];

Now if we have a requirement to select a particular object in the array. Let us assume that we want to find index of student with name Tanmay.

We can do that by iterating through the array and comparing value at the given key.


function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {

for (var i = 0; i < arraytosearch.length; i++) {

if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}

You can use the function to find index of a particular element as below,


var index = functiontofindIndexByKeyValue(studentsArray, "name", "tanmay");
alert(index);

In this way you can find index of an element in JavaScript array of object. I hope you find this quick post useful. Thanks for reading.