Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/rails.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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