The correct way to delegate the click biding here is the following:
- you take your original function (e.g using
valueAccessor()
) - create a new valueaccessor function where you return a new function containing your condition which then calls your original function. And pass this new valueaccessor to the
click
binding.
So your safeClick
will look like this:
ko.bindingHandlers.safeClick = {
'init': function (element, valueAccessor, allBindingsAccessor,
viewModel, bindingContext) {
var originalFunction = valueAccessor();
var newValueAccesssor = function() {
return function () {
if (mycondition == true)
//pass through the arguments
originalFunction.apply(viewModel, arguments);
}
}
ko.bindingHandlers.click.init(element, newValueAccesssor,
allBindingsAccessor, viewModel, bindingContext);
}
}
Demo JSFiddle.
This is will work for the first click, and you don't have to manually subscribe on the click event or trigger it with jQuery.