I would like to create a conditional click binding in KnockoutJS. This is basically standard click binding, as if you were using it in Knockout, but the condition must be met in order for the attached function to be executed. In my case, the best option is to create a custom binding descriptor, in which you then call the standard click binding, if enabled.
ko.bindingHandlers.safeClick = { 'init': function(element, valueAccessor, allBindingsAccessor, context) { $(element).click(function() { if(mycondition == true) {
I want to replace all my standard click bindings with this custom binding. Therefore, it is important to call click bindings in the right way, so all parameters provided in HTML are passed to the function. For instance:
<a href="#" data-bind="click: basevm.gotopage.bind($data, '#homepage')">Home</a> <a href="#" data-bind="click: itemvm.activateItem">Activate</a>
They must be replaced by
<a href="#" data-bind="safeClick: basevm.gotopage.bind($data, '#homepage')">Home</a> <a href="#" data-bind="safeClick: itemvm.activateItem">Activate</a>
I would really appreciate it if you could help me with the missing part in the user binding.
source share