Created
          March 7, 2012 07:34 
        
      - 
      
- 
        Save tarnacious/1991640 to your computer and use it in GitHub Desktop. 
    backbone example - serialize form + update on change
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Great, thanks guys.