I noticed such a behavior of, let's say, "common" (e. g., "enabled") binding and event binding: when bound to a function this context differs inside it.
For example, I add 2 functions to ObservableArray's prototope:
1) logTihis: simply logs this pointer to the console
console.log(
this
);
2) enableDelete: returns whether the array has more than 1 items (assume, I want to prevent deletion of the last item)
return
this
.length > 1;
and a template to render repeating items ("items" field of the view model is set to an array):
<
tr
>
<
td
>#= name #</
td
>
<
td
><
button
data-role
=
"button"
data-bind
=
"enabled: parent().enableDelete, click: parent().logThis"
>Delete</
button
>
</
tr
>
When I write "parent().functionName" I expect the function to be called in the context of what parent() returns (items array in this case).
With enabed binding it is so (having 1 item in the array disables the button), while with click binding it is not so (this pointer in the click handler (logThis) is set to the item itself).
After checking the source code I found out that Binding.get does look for the actual context before calling the function while EventBinding.get does not.
Why is it so? Is not it about to be fixed?
Thank you in advance.