Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Knockout Click binding and the ‘this’ context

DZone's Guide to

Knockout Click binding and the ‘this’ context

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

When using Knockout JS wiring to the click binding is cake and simply just works.  Or does it?

Per the documentation there are multiple ways to wire the click binding within your html.  Assume you have a method such as (the code below is TypeScript)

    public AddMember(member: MemberViewModel) {  
        var self = this;  
      
        // method body here   
    }  

You can do any of the following:

    data-bind=”click: myFunction()” – this will pass in the current $data instance in by default  
      
    data-bind=”click: function(data, event){ myFunction($data, data, event)}” – this will manually pass in the current $data instance  
      
    data-bind=”click: myfunction.bind($data)”  

All of the above will accomplish the same goal, passing in the current $data object into the underlying method.  However, what appears to be the same is actually quite different.  The difference comes in when you are going to use the ‘this’ context.  Lets take a deeper look.

‘this’ context for option 1:

http://s3.amazonaws.com/Devlicious/CommunityServer.Blogs.Components.WeblogFiles/derik_whittaker/image_2DD552DF.png?AWSAccessKeyId=0KMA35HT86EVXB99Z302&Expires=1367274645&Signature=UnL%2fmsGYECTheQgtQjS9FdTJZWg%3d

‘this’ context for option 2:

http://s3.amazonaws.com/Devlicious/CommunityServer.Blogs.Components.WeblogFiles/derik_whittaker/image_48A4099C.png?AWSAccessKeyId=0KMA35HT86EVXB99Z302&Expires=1367274664&Signature=ZLLuWuScoo6ZXmSLYMj47p8a4ms%3d

‘this’ context for option 3:

http://s3.amazonaws.com/Devlicious/CommunityServer.Blogs.Components.WeblogFiles/derik_whittaker/image_7CD832E2.png?AWSAccessKeyId=0KMA35HT86EVXB99Z302&Expires=1367274675&Signature=fZU%2fVCzJVJGa5e252f9a8op4SBQ%3d

Did you notice the difference?  2 of the options (1 & 3) set the ‘this’ context equal to the passed in $data object.  Option 2 has the context set to the $parent object (in our case the view model).

Now I am NOT saying which one is right and which one is wrong because they are all right.  However, I just want to make sure everyone is aware of the differences.  In many cases i use option 2 because I want direct access to the underlying view model and this provides this.

I am sure there are many different ways in JavaScript/Knockout to get the parent View Model but honestly I like low friction and option 2 provides this for me.

Till next time.



Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Derik Whittaker, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}