I would like to create a 'conditional click' binding in KnockoutJS. Basically this is a standard click binding like you would use it in Knockout, but a condition needs to be met in order for the attached function to be executed. In my case, the best option is to create a custom bindinghandler in which you then call the standard click binding if allowed.
ko.bindingHandlers.safeClick = {
'init': function(element, valueAccessor, allBindingsAccessor, context) {
$(element).click(function() {
if(mycondition == true) {
// call the standard click binding here -> this is the part I don't know
}
});
}
}
I want to replace all of my standard click bindings with this custom binding. Therefore it is important to call the click binding in the right way, so all of the parameters provided in HTML are passed through to the function. For example:
<a href="#" data-bind="click: basevm.gotopage.bind($data, '#homepage')">Home</a>
<a href="#" data-bind="click: itemvm.activateItem">Activate</a>
These need to 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 very much appreciate it if you could help me with the missing part in the custom binding.