0

I have a Backbone view with a form. When the user submits a form there can be a validation error, that is handled by the backend. After I display the error, I want the form to be usable again. I thought I should just do this.delegateEvents(), but for some reason it did not work...

Here is the code:

class App.Views.PlotsCreate extends Backbone.View
  template: JST['plots/create']

    events:
        'submit #new_plot': 'createPlot'


    createPlot: (event) ->
      event.preventDefault()
      @attributes = plot: {name: $('#new_plot_name').val(), docreate: "true", code: code }
      @collection.create @attributes,
        wait: true
        success: (plot) => 
          document.body.style.cursor = 'default'
            @showProgress(plot)
        error: @handleError
      )

    handleError: (entry, response) =>
      if response.status == 422
        errors = $.parseJSON(response.responseText).errors
        for attribute, messages of errors
          $('#new_plot_name').val("#{message}" for message in messages)



    <form class="new_plot" name="create_form" id ="new_plot"  data-remote="true" enctype="multipart/form-data">
      <textarea class="input" id="new_plot_name" name="name" rows="5" maxlength = '140'></textarea> 
      <input class="blue_button btn_generate" name="commit" type="submit" value="create" id ="plot_subm"/>
    </form>   

How do I make the form become functional again (i.e. submit event firing again after the error was displayed)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stpn
  • 6,202
  • 7
  • 47
  • 94

1 Answers1

1

Failing validation doesn't normally do anything to event bindings; in fact, technically the view doesn't even have a validate method, only models do (and all that method does is trigger an 'error' event and return false). Are you sure you don't have some code on your end that's breaking the view?

machineghost
  • 33,529
  • 30
  • 159
  • 234
  • the code is here.. it's just that I cannot submit the form second time after the error is returned. – Stpn Nov 08 '12 at 18:56
  • Well, clearly not all of it is; you've got some template thing in your view that couldn't possibly be doing anything without some sort of outside templating library (or Backbone plug-in). But you're missing the point, which is that *Backbone* isn't doing anything: some code you've put on top of Backbone (either a 3rd party library like your templating library or your own code) is the source of the problem. – machineghost Nov 08 '12 at 19:04
  • 2
    P.S. I noticed you've also got `data-remote="true"` which Google reveals to be some crazy Rails thing; it's entirely possible that any Rails JS involved might be the culprit (Rails didn't have JS back when I used it, but I haven't used it in awhile so maybe now it does?). – machineghost Nov 08 '12 at 19:08
  • I think you might be on to something with the `date-remote` stuff, having both the Rails AJAX "friendly" helpers and Backbone both trying to handle form submits is just asking for it. I'd go a step further and say that using the Rails form building stuff with Backbone is a mistake. – mu is too short Nov 08 '12 at 19:42
  • omg, I just saw that I am actually disabling the button with jquery after the submit... I guess I will accept this answer, since the problem was with the non-pasted code indeed. – Stpn Nov 08 '12 at 20:20