I'm making a web form for editing some objects. I'll call these objects Foos.

I have a Backbone model that represents Foos. I have a Backbone view that renders an editor form, filling in fields based on the state of the Foo model it is given.

My goal is to keep the Foo model in sync with the form fields.

Idea 1: I could wire up onkeyup or onchange listeners on all of the input boxes. Whenever the user changes a field, the model would be immediately updated.

Manually adding an event listener to each input box seems excessive, though.

Idea 2: I could add an updateModel method to my view. Calling this method would inspect the form fields and bring the model up to date.

The problem with this is that if you forget to call updateModel, you will be working with a stale model.

What are some best practices for syncing a Backbone model with its user-editable view?

  • 2
    This is usually called data binding. Google "backbone data binding". – Mike Partridge Feb 26 '14 at 20:28
  • epoxy.js might be what I'm looking for – jcarpenter2 Feb 26 '14 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.