diff options
Diffstat (limited to 'railties/lib/generators/rails/app/templates/public/javascripts/jquery.driver.js')
-rw-r--r-- | railties/lib/generators/rails/app/templates/public/javascripts/jquery.driver.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/railties/lib/generators/rails/app/templates/public/javascripts/jquery.driver.js b/railties/lib/generators/rails/app/templates/public/javascripts/jquery.driver.js new file mode 100644 index 0000000000..7df5f44c2c --- /dev/null +++ b/railties/lib/generators/rails/app/templates/public/javascripts/jquery.driver.js @@ -0,0 +1,48 @@ +jQuery(function ($) { + $('form[data-remote="true"]').live('submit', function (e) { + var form = $(this), + data = [], + condition = form.attr('data-condition') ? eval(form.attr('data-condition')) : true; + + + if (form.attr('data-submit')) { + data = $('#' + form.attr('data-submit')).serializeArray(); + } else if (form.attr('data-with')) { + data = form.attr('data-with'); + } else { + data = form.serializeArray(); + } + + if(condition) { + form.trigger('before'); + + $.ajax({ + async: form.attr('data-remote-type') === 'synchronous' ? false : true, + url: form.attr('action'), + method: form.attr('method'), + data: data, + beforeSend: function (xhr) { + form.trigger('after', xhr); + form.trigger('loading', xhr); + }, + success: function (data, status, xhr) { + var update = form.attr('data-update-success'); + form.trigger('success', [data, status, xhr]); + + if (update) { + $(update + ', #' + update).html(data); + } + }, + complete: function (xhr) { + form.trigger('complete', xhr); + form.trigger('loaded', xhr); + }, + error: function (xhr, status, error) { + form.trigger('failure', [xhr, status, error]); + } + }); + } + + e.preventDefault(); + }); +}); |