Two-way data binding means that when the properties of an object change, the corresponding UI can be changed at the same time, and vice versa. In other words, if we have a user object that has a name property, whenever you set a new value to user.name, the UI will display the new value. Likewise, if the UI contains an input box for the user's name, entering a new value will cause the user object's name property to change accordingly.
Many popular javascript frameworks, like Ember.js, Angular.js or KnockoutJS, promote two-way data binding as one of their main features. This does not mean that implementing it from scratch is difficult, nor does it mean that using these frameworks is our only option when we need this functionality. The underlying idea inside is actually quite basic, and its implementation can be summarized in the following three points:
Although there are many ways to achieve these points, a simple and efficient way is that we implement it through the publish-subscriber pattern. The method is simple: we can use the customized data attribute as the attribute that needs to be bound in the HTML code. All JavaScript objects and DOM elements that are bound together will subscribe to this publish-subscribe object. Any time we detect a change in either a Javascript object or an HTML input element, we pass the event proxy to the publish-subscribe object, and then pass and broadcast all changes that occur in the bound objects and elements through it. go out.
A simple example implemented with jQuery
It is quite simple and straightforward to implement what we discussed above through jQuery, because as a popular library, it allows us to easily subscribe to and publish DOM events, and we can also customize one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
As for JavaScript objects, here is an example of a minimal user data model implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Now, whenever we want to bind the properties of an object to the UI, we just set the appropriate data attribute on the corresponding HTML element.
1 2 3 4 5 6 |
|
Value changes in the input box will automatically be mapped to the user's name attribute, and vice versa. You're done!
An implementation that does not require jQuery
Most projects nowadays generally use jQuery, so the above example is completely acceptable. But what if we need to be completely independent of jQuery? Well, in fact it is not difficult to do (especially when we only provide support for IE8 and above). Finally, we just have to observe DOM events through the publish-subscriber pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
The data model can remain unchanged, except for the call to the trigger method in jQuery in the setter, which can be replaced by our customized publish method in PubSub.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
We explained it through examples, and once again achieved the results we wanted with less than a hundred lines of maintainable pure JavaScript. We hope it will be helpful to everyone in realizing two-way binding of JavaScript data.