Closed danieldickison closed 3 years ago
@danieldickison I think this scratches an itch a few people will have. 😀
Using a helper-function like
data-bind="click: utils.preventDefault(event => setSomeValue('foo'))"
would be a more terse variant. The exact semantics from knockout could in this case be retained.
Of course, if preventDefault
needs to be observable we might need to do it like
data-bind="click: event => { setSomeValue('foo')); return $data.shouldPreventDefault; }"
A third alternative, and in my opinion more semantically correct, is to create your own binding handler "capturedClick" or something like that, where you describe the functionality instead of implementing/open-coding it in the view.
Isn't this what we have a viewModel for? This will put logic into the view which has always been avoided in knockout which has been one of the main reasons to prefer it from any other framework or library.
This will also be very confusing when reusing an even handler method. Will the method depend on the view to preventDefault?
Also, what about stopPropagation() and stopImmediatePropagation()? These functions and preventDefault() is functions, not options.
I tend to think there are two "correct" solutions here.
A third alternative, and in my opinion more semantically correct, is to create your own binding handler "capturedClick" or something like that, where you describe the functionality instead of implementing/open-coding it in the view.
No library changes would be needed for this.
Also, what about stopPropagation() and stopImmediatePropagation()? These functions and preventDefault() is functions, not options.
in which case stopPropagation
and stopImmediatePropagation
ought to be parameters in the library as well.
There's nothing stopping folks from using a function e.g. utils.preventDefault
but that doesn't require any library changes.
Closing this as I trust it's closed, but we can always re-open if there's more to discuss here.
Because lambda (arrow functions) in bindings currently only support a single expression, the old pattern of returning
true
from a (e.g.) click handler is a bit inconvenient:Adding a
preventDefault
key to an event handler was discussed in #32 but it doesn't appear to have been implemented. Does this still sound like a good idea?