4 ms·
It is a shame to conclude two way data-binding should be a casualty in the maturation of frameworks. Like one way data-binding, it can be misused. Two-way data
by AdamCraven 12y ago
It is a shame to conclude two way data-binding should be a casualty in the maturation of frameworks. Like one way data-binding, it can be misused.
Two-way data binding to properties is generally not good. It increases likelihood of problems as updates to one value have to be observed to adjust other values. It is easy for bugs to slip in and hard to reason about the code.
Two-way data binding to methods is better. Methods that aren't focused on setting one value. In effect not direct data binding. It creates easy to reason about explicit code paths for changing of values. Observation of the change of properties is no longer required.
It is a useful tool and should continue to be seen as such.
- estava 12y agoWhen I was creating a UI library, I tried to pick up on the trend of data-binding, but I found it difficult to use. Now these discussions remind me of it. I don't even know how the second option is better to be honest. I may research it further. :-)
- danabramov 12y agoWhat is two-way binding to a method? Can you give me an example? I can only think of this[1] but despite being similar to two-way binding, this is one-way and explicit. [1]: http://facebook.github.io/react/docs/two-way-binding-helpers.html http://facebook.github.io/react/docs/two-way-binding-helpers...
- jdmichal 12y agoI believe the intention is basically "bind to a setter method, not a field." I'm guessing for much the same reasons the same is used in Java: You can change methods. You can add sanity checks, new behavior, etc.
- AdamCraven 12y agoWhat you are linking to is still two-way data binding. Which enforces a one-way flow of data. Two-way binding: Read & Write. Read & Write happens through getter/setter methods. One-way binding: Read only. Writes happen by external event listeners. Both can be used to enforce one way flow of data. Just as one way binding can be used to have a uni-directional flow of data. A pseudo code example with two-way data binding with one-way flow of data: <input type="text" value=getOrSetValue() /> function getOrSetValue(newValue) { if (newValue) { // Send an event to a dispatcher with updated value. } else { return value // can be single value from model or derived value, such as calculation of multiple values. } }