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)
You can do any of the following:
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:
‘this’ context for option 2:
‘this’ context for option 3:
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.
Till next time.
04-29-2013 8:05 AM