Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read data from a form in backbone

Tags:

backbone.js

I'm working on a Backbone application, and I have almost everything working, except the way I should read data from a form.

I have a few views and templates that create the form elements, and a "save" button. When I click that button it fires a view event, and there is where I would like to know if there is a "best practice" to read that info.

The options I was thinking were:

  • Reading the inputs using jQuery, like jQuery('#name').val(), which would be annoying if you have a big form.

  • Using serializeArray

  • The last one would be something like https://github.com/derickbailey/backbone.syphon, but not sure how popular is it.

So, which is the best "backbone way" to do it?

Thanks a lot!

like image 515
Mustela Avatar asked Feb 22 '13 00:02

Mustela


1 Answers

What is the best way is hard to say as it depends.

The example you show in your post are all meant for one action: submitting a form to a backend. Then, syphon (made by a great developper) and serializeArray (or just $(form).serialize()) are great solution (obviously the first one isn't great for a big form - so I'd forget this one).

Although, if your form is meant to update a model in realtime, and that this form should reflect change to model. Then, I'd go for a data-binding library. The best I know is Backbone.stickit; there's also Backbone.ModelBinder. I used both and they're great, although the first one use a little bit less boilerplate.

like image 106
Simon Boudrias Avatar answered Oct 16 '22 02:10

Simon Boudrias