Skip to content

Instantly share code, notes, and snippets.

@lporras
Created April 17, 2012 03:00
Show Gist options
  • Save lporras/2403098 to your computer and use it in GitHub Desktop.
Save lporras/2403098 to your computer and use it in GitHub Desktop.
Using Backbone Views With Rails jQuery-ujs
var FormView = Backbone.View.extend({
el: '#form',
events: {
// Fired automatically when a file-type input is detected with a
// non-blank value. You can use this hook to implement a handler that
// will deal with those non-blank file inputs. Returning false will
// disallow standard form submission.
'ajax:aborted:file' : 'ajaxAbortedFile',
// Fired when there are required inputs which have been left blank.
// You can use this handler to deal with those blank required inputs.
// Returning false will submit the form anyway.
'ajax:aborted:required' : 'ajaxAbortedRequired',
// First event fired for any remote enabled form. Stopping this event
// will cancel the ajax request
'ajax:before' : 'ajaxBefore',
// Fired before the ajax request is sent. Stopping this event will
// cancel the ajax request. Commonly used to customize certain request
// headers
'ajax:beforeSend' : 'ajaxBeforeSend',
// Fired after completion, if the HTTP response was a success
'ajax:success' : 'ajaxSuccess',
// Fired after completion, if the server returned an error
'ajax:error' : 'ajaxError',
// Fired after the request has been completed, no matter what outcome
'ajax:complete' : 'ajaxComplete'
},
ajaxAbortedFile: function(e, elements){
},
ajaxAbortedRequired: function(e, elements){
},
ajaxBefore: function(e){
},
ajaxBeforeSend: function(e, xhr, settings){
},
ajaxSuccess: function(e, data, status, xhr){
},
ajaxError: function(e, xhr, status, error){
},
ajaxComplete: function(e, xhr, status){
}
});
$(function(){
window.view = new FormView();
});
<form id="form" action="#" method="POST" data-remote="true">
<p><input type="text" value="..."></p>
<p><input type="submit" value="Continue &rarr;"></p>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment