Knockout: Event binding doesn't fail at binding time if function doesn't exist

Created on 23 Feb 2017  路  6Comments  路  Source: knockout/knockout

If I pass a non-existing function to event binding, it will fail only at runtime, not at binding-time. This can lead to hidden errors if the application is not completely covered during testing. As opposed to this, for example the submit binding does check the bound value, and throws an immediate error if it's not a function.

The "silent" failure is caused by this line in event binding.

var handlerFunction = valueAccessor()[eventName];
if (!handlerFunction)
  return;

Is this behavior intentional? If not, I'd like to prepare a PR adding a check to event binding similar to submit binding.

average minor bug

Most helpful comment

After some thinking I still think that it's an issue that knockout won't throw a binding error when I pass a function where I made a typo for example.

Probably we should introduce a check before hooking up the event handlers, and allow there explicit null values, but not anything else.

All 6 comments

I'd like to prepare a PR adding a check to event binding similar to submit binding.

That'd be great. Thanks.

@mbest Checking the specs it seems like this behavior was indeed intentional, there is an explicit test for passing null to mouseout event, and the last line of the related test states that it shouldn't throw (when triggering mouseout event). Can you recall what was the purpose of this? for me it doesn't make sense at all at first glance.

I ment this exact line: https://github.com/knockout/knockout/blob/master/spec/defaultBindings/eventBehaviors.js#L26

It seems like this is related to the fact that the valueAccessor is reevaluated in each event handler call, so I guess it allows us to dynamically suspend event handling on some events. But the use case for this is not clear at first glance, and I can't even find any note about this in the docs.

so I guess it allows us to dynamically suspend event handling on some events

Hmm. You may be right. I remember this coming up before as a method of suspending events.

I tracked the change down to #73, so this has been around a while (before I was involved).

After some thinking I still think that it's an issue that knockout won't throw a binding error when I pass a function where I made a typo for example.

Probably we should introduce a check before hooking up the event handlers, and allow there explicit null values, but not anything else.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrewchch picture andrewchch  路  3Comments

Umar-Mukhtar picture Umar-Mukhtar  路  8Comments

aminroosta picture aminroosta  路  5Comments

Apollo3zehn picture Apollo3zehn  路  3Comments

brunolau picture brunolau  路  8Comments