My question is regarding the click binding in Knockout, and why the following behaviour occurs. I have found many click binding questions on here, but not specific to the behaviour I describe below.
I understand that in Knockout click binding with parameters are executed on page load as Knockout expects a reference (to a function) so when it comes across a function call within a binding it will execute that function expecting a returned function reference to bind to. So if I returned another reference to a function, it would be that function which is executed on the elements click.
So far so good, that makes sense.
In order to see this for myself, I quickly created this:
HTML:
<input data-bind="click: selectImportType(1)" type="button" />
JS
var functiontest = function () {
alert('test');
};
viewModel.selectImportType = function (type) {
viewModel.selectedImport(type);
return functiontest;
}
Upon executing this, I have found that the 'functiontest' reference is returned to the binding as the click calls the functiontest function as expected.
Now to my confusion. I found that the selectImportType function is also called when the element is clicked. selectImportType is called first followed by a call to the functiontest function.
How is this possible? The reference resolved during the binding was to the functiontest function!