Skip to content

Instantly share code, notes, and snippets.

@aliang aliang/user_form.html
Last active Dec 29, 2015

Embed
What would you like to do?
<form>
Name: <input type="text" name="name"/>
Age: <input type="text" name="age"/>
<input type="submit" value="Submit"></input>
</form>
var UserForm = Backbone.View.extend({
events: {'submit': 'save'},
initialize: function() {
_.bindAll(this, 'save');
},
save: function() {
// http://api.jquery.com/serializeArray/
var arr = this.$el.serializeArray();
// This accumulates the name/value hashes into a single hash which is much easier to submit to forms
var data = _(arr).reduce(function(acc, field) {
acc[field.name] = field.value;
return acc;
}, {});
this.model.save(data);
return false;
}
});
var userForm = new UserForm({el: this.$('form'), model: new User()});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.