What is two way binding?

后端 未结 5 1325
后悔当初
后悔当初 2020-11-27 09:32

I have read lots that Backbone doesn\'t do two way binding but I don\'t exactly understand this concept.

Could somebody give me an example of how two way binding wor

5条回答
  •  北海茫月
    2020-11-27 09:34

    Two-way binding just means that:

    1. When properties in the model get updated, so does the UI.
    2. When UI elements get updated, the changes get propagated back to the model.

    Backbone doesn't have a "baked-in" implementation of #2 (although you can certainly do it using event listeners). Other frameworks like Knockout do wire up two-way binding automagically.



    In Backbone, you can easily achieve #1 by binding a view's "render" method to its model's "change" event. To achieve #2, you need to also add a change listener to the input element, and call model.set in the handler.

    Here's a Fiddle with two-way binding set up in Backbone.

提交回复
热议问题