2

In writing some applications in backbone js I found myself doing very well with backbone. It was the forms/templates that were killing me. It would take me 15 minutes to work with the model,collection and views; then an hour or 2 to work on the template. The reason for this is my forms are dynamic, select boxes auto populate with the current model value selected, text boxes have the model value in it already.

I am currently trying to write a couple of utility functions to write a form dynamically for me; the capabilities I would like it to have are:

  1. Take a model's attributes and create a blank form based on the form.
  2. Populate all select fields with possible values
  3. Populate the form with selected values(model attributes) (this will probably be the same function as number 2, just to do it all in one swoop)
  4. Read in the form values and set model attributes accordingly

My question really boils down to two parts, first does anyone have a better solution? Secondly what would be the best way to implement the configuration of these functions? Would it be best to have a variable with JSON in it?

var myModelConfiguration =
    [
        {"field":"userName","type":"text","showInForm":1},
        {"field":"userRole","type":"select","showInForm":1}
    ]

Or would it be better to create in a model that would just hold all of the configuration and field types in it; and just pass the configuration model in as well as the data model?

Any advice on this method or a better method would be very appreciated. Thanks.

1

1 Answer 1

2

Have you looked into backbone-forms? I find it very handy for form generation. Of course, I've written my own extended functionality on top of it, but it meets your four criteria by itself.

1
  • 1
    Oh wow, backbone-forms is exactly what I was looking for. This is magical.
    – cgenco
    Sep 23, 2013 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.