From e78f71be266690b07c3d7b9206bcb8bb34b1b38a Mon Sep 17 00:00:00 2001 From: Mathieu Martin Date: Wed, 7 Mar 2012 16:10:00 -0500 Subject: [PATCH] Make it possible to override the way we get the href, add test for it. --- src/rails.js | 9 +++++++-- test/public/test/override.js | 37 ++++++++++++++++++++++++++++++++++++ test/views/index.erb | 2 +- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 test/public/test/override.js diff --git a/src/rails.js b/src/rails.js index 06b4e0b5..bc277322 100644 --- a/src/rails.js +++ b/src/rails.js @@ -99,6 +99,11 @@ return $.ajax(options); }, + // Default way to get an element's href. May be overridden at $.rails.href. + href: function(element) { + return element.attr('href'); + }, + // Submits "remote" forms and links with ajax handleRemote: function(element) { var method, url, data, @@ -125,7 +130,7 @@ if (element.data('params')) data = data + "&" + element.data('params'); } else { method = element.data('method'); - url = element.attr('href'); + url = rails.href(element); data = element.data('params') || null; } @@ -160,7 +165,7 @@ // Handles "data-method" on links such as: // Delete handleMethod: function(link) { - var href = link.attr('href'), + var href = rails.href(link), method = link.data('method'), target = link.attr('target'), csrf_token = $('meta[name=csrf-token]').attr('content'), diff --git a/test/public/test/override.js b/test/public/test/override.js new file mode 100644 index 00000000..a5dce2c6 --- /dev/null +++ b/test/public/test/override.js @@ -0,0 +1,37 @@ +(function(){ + +module('override', { + setup: function() { + window.realHref = $.rails.href; + $('#qunit-fixture').append($('', { + href: '/real/href', 'data-method': 'delete', 'data-href': '/data/href' + })); + }, + teardown: function() { + $.rails.href = window.realHref; + } +}); + +asyncTest("the getter for an element's href is publicly accessible", 1, function() { + ok($.rails.href); + start(); +}); + +asyncTest("the getter for an element's href is overridable", 1, function() { + $.rails.href = function(element) { return element.data('href'); } + $.rails.ajax = function(options) { + equal('/data/href', options.url); + } + $.rails.handleRemote($('#qunit-fixture').find('a')); + start(); +}); + +asyncTest("the getter for an element's href works normally if not overridden", 1, function() { + $.rails.ajax = function(options) { + equal('/real/href', options.url); + } + $.rails.handleRemote($('#qunit-fixture').find('a')); + start(); +}); + +})(); diff --git a/test/views/index.erb b/test/views/index.erb index 21c7bc6c..2364f06f 100644 --- a/test/views/index.erb +++ b/test/views/index.erb @@ -1,6 +1,6 @@ <% @title = "jquery-ujs test" %> -<%= test 'data-confirm', 'data-remote', 'data-disable', 'call-remote', 'call-remote-callbacks', 'data-method' %> +<%= test 'data-confirm', 'data-remote', 'data-disable', 'call-remote', 'call-remote-callbacks', 'data-method', 'override' %>

<%= @title %>