Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

backbone example - serialize form + update on change

View myview.js.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
window.MyView = Backbone.View.extend({
initialize: ->
_.bindAll(this,'render')
this.template = window.JST["MyView"]
this.model.bind('change', this.render)
 
render: ->
$(this.el).html(this.template(this.model.toJSON()))
 
events: {
'change':'update'
}
update: ->
form = this.$("form").serializeObject()
this.model.set(form)
View myview.js.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
jQuery.fn.serializeObject = ->
arrayData = @serializeArray()
objectData = {}
 
$.each arrayData, ->
if @value?
value = @value
else
value = ''
 
if objectData[@name]?
unless objectData[@name].push
objectData[@name] = [objectData[@name]]
 
objectData[@name].push value
else
objectData[@name] = value
 
return objectData

I found it helpful to compile templates into JavaScript, so that they don't have to be included in the page https://gist.github.com/1977676

Great, thanks guys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.