Skip to content

Commit

Permalink
Merge pull request #251 from webmat/issue-246
Browse files Browse the repository at this point in the history
Make it possible to override the way we get the href, add test for it.
  • Loading branch information
Neeraj Singh committed Mar 29, 2012
2 parents 6186a85 + e78f71b commit 2e5b8c4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/rails.js
Expand Up @@ -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,
Expand All @@ -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;
}

Expand Down Expand Up @@ -160,7 +165,7 @@
// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
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'),
Expand Down
37 changes: 37 additions & 0 deletions test/public/test/override.js
@@ -0,0 +1,37 @@
(function(){

module('override', {
setup: function() {
window.realHref = $.rails.href;
$('#qunit-fixture').append($('<a />', {
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();
});

})();
2 changes: 1 addition & 1 deletion 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' %>

<h1 id="qunit-header"><%= @title %></h1>
<div id="jquery-cdn">
Expand Down

0 comments on commit 2e5b8c4

Please sign in to comment.