I am using knock out for my binding purposed and i am working on a share module. basically, there are couple of links and they all are basically calling the same function just with different values. For example, when you click on the Facebook then all the service with value “Facebook” and when and when you click on Twitter then call the function with the value “Twitter”. But basically calling the same function. Now that there is not way to specify parameters from HTMl because all I can do is something like data-bind=”click: Share”. Here we cans see that I can not specify any parameters here.
Now I can create an ko.observable in my js file which this function will take as an argument… but I need a way to write on that observable from html click event when different links are clicked. Any idea on how to do it?
Knockout passes the current context to any function used in data binding. This means that if you call
click: shareit will pass in the service you have bound. Here is a fiddle demonstrating this, but I’ll put in the code here since its pretty basic.For more complex bindings, your object will likely have a
nameproperty, which you can use in place of$data, as well as use in yourshare()function.HTML:
Javascript: