diff options
Diffstat (limited to 'railties/lib/rails')
-rw-r--r-- | railties/lib/rails/generators/rails/app/templates/public/javascripts/jquery_ujs.js | 74 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/app/templates/public/javascripts/prototype_ujs.js | 72 |
2 files changed, 91 insertions, 55 deletions
diff --git a/railties/lib/rails/generators/rails/app/templates/public/javascripts/jquery_ujs.js b/railties/lib/rails/generators/rails/app/templates/public/javascripts/jquery_ujs.js index 43456a77f6..668cffa73a 100644 --- a/railties/lib/rails/generators/rails/app/templates/public/javascripts/jquery_ujs.js +++ b/railties/lib/rails/generators/rails/app/templates/public/javascripts/jquery_ujs.js @@ -1,3 +1,12 @@ +/* + * jquery-ujs + * + * http://github.com/rails/jquery-ujs/blob/master/src/rails.js + * + * This rails.js file supports jQuery 1.4.3 and 1.4.4 . + * + */ + jQuery(function ($) { var csrf_token = $('meta[name=csrf-token]').attr('content'), csrf_param = $('meta[name=csrf-param]').attr('content'); @@ -7,9 +16,6 @@ jQuery(function ($) { * Triggers a custom event on an element and returns the event result * this is used to get around not being able to ensure callbacks are placed * at the end of the chain. - * - * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our - * own events and placing ourselves at the end of the chain. */ triggerAndReturn: function (name, data) { var event = new $.Event(name); @@ -19,26 +25,33 @@ jQuery(function ($) { }, /** - * Handles execution of remote calls firing overridable events along the way + * Handles execution of remote calls. Provides following callbacks: + * + * - ajax:beforeSend - is executed before firing ajax call + * - ajax:success - is executed when status is success + * - ajax:complete - is executed when the request finishes, whether in failure or success + * - ajax:error - is execute in case of error */ callRemote: function () { var el = this, method = el.attr('method') || el.attr('data-method') || 'GET', url = el.attr('action') || el.attr('href'), - dataType = el.attr('data-type') || 'script'; + dataType = el.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType); if (url === undefined) { - throw "No URL specified for remote call (action or href must be present)."; + throw "No URL specified for remote call (action or href must be present)."; } else { - if (el.triggerAndReturn('ajax:before')) { - var data = el.is('form') ? el.serializeArray() : []; + var $this = $(this), data = el.is('form') ? el.serializeArray() : []; + $.ajax({ url: url, data: data, dataType: dataType, type: method.toUpperCase(), beforeSend: function (xhr) { - el.trigger('ajax:loading', xhr); + if ($this.triggerHandler('ajax:beforeSend') === false) { + return false; + } }, success: function (data, status, xhr) { el.trigger('ajax:success', [data, status, xhr]); @@ -47,20 +60,17 @@ jQuery(function ($) { el.trigger('ajax:complete', xhr); }, error: function (xhr, status, error) { - el.trigger('ajax:failure', [xhr, status, error]); + el.trigger('ajax:error', [xhr, status, error]); } }); - } - - el.trigger('ajax:after'); } } }); /** - * confirmation handler + * confirmation handler */ - $('a[data-confirm],input[data-confirm]').live('click', function () { + $('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click.rails', function () { var el = $(this); if (el.triggerAndReturn('confirm')) { if (!confirm(el.attr('data-confirm'))) { @@ -70,28 +80,34 @@ jQuery(function ($) { }); + /** * remote handlers */ - $('form[data-remote]').live('submit', function (e) { + $('form[data-remote]').live('submit.rails', function (e) { $(this).callRemote(); e.preventDefault(); }); - $('a[data-remote],input[data-remote]').live('click', function (e) { + $('a[data-remote],input[data-remote]').live('click.rails', function (e) { $(this).callRemote(); e.preventDefault(); }); - $('a[data-method]:not([data-remote])').live('click', function (e){ + /** + * <%= link_to "Delete", user_path(@user), :method => :delete, :confirm => "Are you sure?" %> + * + * <a href="/users/5" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a> + */ + $('a[data-method]:not([data-remote])').live('click.rails', function (e){ var link = $(this), href = link.attr('href'), method = link.attr('data-method'), form = $('<form method="post" action="'+href+'"></form>'), metadata_input = '<input name="_method" value="'+method+'" type="hidden" />'; - if (csrf_param != null && csrf_token != null) { - metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />'; + if (csrf_param !== undefined && csrf_token !== undefined) { + metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />'; } form.hide() @@ -105,9 +121,9 @@ jQuery(function ($) { /** * disable-with handlers */ - var disable_with_input_selector = 'input[data-disable-with]'; - var disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')'; - var disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')'; + var disable_with_input_selector = 'input[data-disable-with]', + disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')', + disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')'; var disable_with_input_function = function () { $(this).find(disable_with_input_selector).each(function () { @@ -118,10 +134,10 @@ jQuery(function ($) { }); }; - $(disable_with_form_remote_selector).live('ajax:before', disable_with_input_function); - $(disable_with_form_not_remote_selector).live('submit', disable_with_input_function); + $(disable_with_form_remote_selector).live('ajax:before.rails', disable_with_input_function); + $(disable_with_form_not_remote_selector).live('submit.rails', disable_with_input_function); - $(disable_with_form_remote_selector).live('ajax:complete', function () { + $(disable_with_form_remote_selector).live('ajax:complete.rails', function () { $(this).find(disable_with_input_selector).each(function () { var input = $(this); input.removeAttr('disabled') @@ -129,4 +145,10 @@ jQuery(function ($) { }); }); + var jqueryVersion = $().jquery; + + if (!( (jqueryVersion === '1.4.3') || (jqueryVersion === '1.4.4'))){ + alert('This rails.js does not support the jQuery version you are using. Please read documentation.'); + } + }); diff --git a/railties/lib/rails/generators/rails/app/templates/public/javascripts/prototype_ujs.js b/railties/lib/rails/generators/rails/app/templates/public/javascripts/prototype_ujs.js index fc69f93c58..4c18cb0c3e 100644 --- a/railties/lib/rails/generators/rails/app/templates/public/javascripts/prototype_ujs.js +++ b/railties/lib/rails/generators/rails/app/templates/public/javascripts/prototype_ujs.js @@ -95,9 +95,10 @@ parameters: params, evalScripts: true, - onComplete: function(request) { element.fire("ajax:complete", request); }, - onSuccess: function(request) { element.fire("ajax:success", request); }, - onFailure: function(request) { element.fire("ajax:failure", request); } + onCreate: function(response) { element.fire("ajax:create", response); }, + onComplete: function(response) { element.fire("ajax:complete", response); }, + onSuccess: function(response) { element.fire("ajax:success", response); }, + onFailure: function(response) { element.fire("ajax:failure", response); } }); element.fire("ajax:after"); @@ -114,7 +115,7 @@ csrf_token = $$('meta[name=csrf-token]')[0]; var form = new Element('form', { method: "POST", action: url, style: "display: none;" }); - element.parentNode.insert(form); + $(element.parentNode).insert(form); if (method !== 'post') { insertHiddenField(form, '_method', method); @@ -127,52 +128,65 @@ form.submit(); } + function disableFormElements(form) { + form.select('input[type=submit][data-disable-with]').each(function(input) { + input.store('rails:original-value', input.getValue()); + input.setValue(input.readAttribute('data-disable-with')).disable(); + }); + } + + function enableFormElements(form) { + form.select('input[type=submit][data-disable-with]').each(function(input) { + input.setValue(input.retrieve('rails:original-value')).enable(); + }); + } - document.on("click", "*[data-confirm]", function(event, element) { + function allowAction(element) { var message = element.readAttribute('data-confirm'); - if (!confirm(message)) event.stop(); - }); + return !message || confirm(message); + } - document.on("click", "a[data-remote]", function(event, element) { - if (event.stopped) return; - handleRemote(element); - event.stop(); - }); + document.on('click', 'a[data-confirm], a[data-remote], a[data-method]', function(event, link) { + if (!allowAction(link)) { + event.stop(); + return false; + } - document.on("click", "a[data-method]", function(event, element) { - if (event.stopped) return; - handleMethod(element); - event.stop(); + if (link.readAttribute('data-remote')) { + handleRemote(link); + event.stop(); + } else if (link.readAttribute('data-method')) { + handleMethod(link); + event.stop(); + } }); - document.on("click", "form input[type=submit]", function(event, button) { + document.on("click", "form input[type=submit], form button[type=submit], form button:not([type])", function(event, button) { // register the pressed submit button event.findElement('form').store('rails:submit-button', button.name || false); }); document.on("submit", function(event) { - var form = event.findElement(), - message = form.readAttribute('data-confirm'); + var form = event.findElement(); - if (message && !confirm(message)) { + if (!allowAction(form)) { event.stop(); return false; } - form.select('input[type=submit][data-disable-with]').each(function(input) { - input.store('rails:original-value', input.getValue()); - input.disable().setValue(input.readAttribute('data-disable-with')); - }); - if (form.readAttribute('data-remote')) { handleRemote(form); event.stop(); + } else { + disableFormElements(form); } }); - document.on("ajax:after", "form", function(event, form) { - form.select('input[type=submit][data-disable-with]').each(function(input) { - input.setValue(input.retrieve('rails:original-value')).enable(); - }); + document.on('ajax:create', 'form', function(event, form) { + if (form == event.findElement()) disableFormElements(form); + }); + + document.on('ajax:complete', 'form', function(event, form) { + if (form == event.findElement()) enableFormElements(form); }); })(); |