What is two way binding? Ask Question

What is two way binding? Ask Question

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 works in an MVC codebase and how it does not with Backbone?

ベストアンサー1

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.

おすすめ記事