Hacker News new | ask | show | jobs
by pluma 3423 days ago
I think the second one isn't really a valid example. There are very few situations where you both want to have state mutations discard what the user has already entered and reflect user-provided state in real time. In other words even if it's supposedly "two-way" you don't actually want multiple inputs to share the same model and you likely don't want to represent the data the same way the form control needs to represent it internally.

As an example, let's say you have a formatted text input field that can be used to enter a decimal number. If the input contains no decimal separator, the decimal part is implicitly zero. But if you just use naive two-way binding with an actual decimal value this means you'll get in the way of the user trying to manually enter a decimal value (especially if the user tries to delete the decimal part starting with the separator).

As soon as any information is lost during the bind in either direction, you still need to explicitly think about where you want the data to flow into and out of the form field.

The archetypical example of two-way binding is definitely the auto-generated CRUD form that lets you edit a model in place, but IMO this is a tiny niche in practice because it falls apart as soon as you want to do anything non-trivial. It's great for prototypes though.

EDIT: To clarify, I think you're talking about two-way binding a form model to form inputs. But calling that two-way binding from an application developer's POV is kinda redundant because you still need changes from the form model to propagate outside the form in a more controllable and predictable way than two-way binding offers, so the two-way binding of the actual form fields becomes an implementation detail.

1 comments

>you just use naive two-way binding with an actual decimal value this means you'll get in the way of the user trying to manually enter a decimal value

Naive one way binding is useless as well.

>two-way binding of the actual form fields becomes an implementation detail.

Kinda. In fact in Vue two-binding is a syntax sugar for one way binding and on change events.

The difference between them, is one requires less lines of code and is less powerful.

If you do need react-style form input, then you can do it that way.