Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js events not being recognized in IE8

I have events for 'submit' on a form. In IE8, if I submit the form without entering any information, just simply click the submit button, it posts the form and it's never caught or handled by the defined event in Backbone. However, if I simply click on an input field, then hit submit the event is handled.

The backbone event is setup like this:

events: {

'submit #form': 'submitForm',

        },
submitForm: function(e){
     e.preventDefault();
}

Any ideas why this would be?

Update: Here's an example of form:

<div id="form">
  <form action="/action" method="post">
  <input type="text" name="name" />
  <button type="submit" value="Submit"></button>
  </form>
</div>

It's literally only in IE8 and ONLY if you don't first click an element within the form, before submitting. The event is triggered in FF, Chrome, IE9+ without a problem. But only in IE8, if you just click submit, without doing anything else, the event doesn't get triggered.

like image 715
dzm Avatar asked Nov 04 '22 19:11

dzm


1 Answers

Some events can't be delegated using this method. 'focus' and 'blur' don't work, and in IE8 and lower 'change', 'submit' and 'reset' don't work either. It's actually in the annotated source of Backbone but for some reason not in the documentation.

JQuery's documentation actually states that the method that Backbone uses ($.delegate) is now superseded by $.on which is able to handle those kinds of events.

So your options are:

  • overload Backbone.View.delegateEvents to use $.on instead of $.delegate
  • manually bind the events as shown by EndangeredMassa.
like image 151
Jaap Rood Avatar answered Nov 09 '22 04:11

Jaap Rood