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
23 changes: 20 additions & 3 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,26 @@ jQuery(function ($) {
* - ajax:complete - is execute when status is complete
* - ajax:failure - is execute in case of error
* - ajax:after - is execute every single time at the end of ajax call
*
* Optional: Accepts an event who's target will be included with the submitted data
*/
callRemote: function () {
callRemote: function (e) {
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';

if (url === undefined) {
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() : [];
if (e && e.target) {
var target = $(e.target);
var name = target.attr('name');
if (name) {
data.push({name: name, value: target.attr('value')});
}
}
$.ajax({
url: url,
data: data,
Expand Down Expand Up @@ -91,8 +99,17 @@ jQuery(function ($) {
/**
* remote handlers
*/

//This allows a multiple button form to work properly, passing the button as a data parameter
$('form[data-remote] input[type=\'submit\'], form[data-remote] button').live('click', function (e) {
$(this).closest('form').callRemote(e);
e.preventDefault();
});

//This is the original handler. Fortunately, both will not fire,
// since the event is prevented from bubbling by the click handler
$('form[data-remote]').live('submit', function (e) {
$(this).callRemote();
$(this).callRemote(e);
e.preventDefault();
});

Expand Down
61 changes: 61 additions & 0 deletions test/public/test/data-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ module('data-remote', {
'value': 'john'
}));

form.append($('<input />', {
id: "team_submit",
type: "submit",
name: "team",
value: "Team 1"
}));

form.append($('<button />', {
id: "team_button",
name: "team",
value: "Team 2"
}));


$('#fixtures').append(form);

}
Expand Down Expand Up @@ -90,3 +104,50 @@ test('Submitting form with data-remote attribute', function() {
.trigger('submit');
});

test('Submitting multi-button form with data-remote attribute using html input', function() {
expect(5);
stop(App.ajax_timeout);

$('form[data-remote]')
.live('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');

var request_env = $.parseJSON(data)['request_env'],
params = request_env['rack.request.query_hash'];

App.assert_request_path(request_env, '/update');
equals(params['user_name'], 'john', 'ajax arguments should have key user_name with right value');
equals(params['team'], 'Team 1', 'ajax arguments should have button key with right value');
App.assert_post_request(request_env);

})

$('form[data-remote] input[type=\'submit\']#team_submit').trigger('click');

start();

});

test('Submitting multi-button form with data-remote attribute using html button', function() {
expect(5);
stop(App.ajax_timeout);

$('form[data-remote]')
.live('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');

var request_env = $.parseJSON(data)['request_env'],
params = request_env['rack.request.query_hash'];

App.assert_request_path(request_env, '/update');
equals(params['user_name'], 'john', 'ajax arguments should have key user_name with right value');
equals(params['team'], 'Team 2', 'ajax arguments should have button key with right value');
App.assert_post_request(request_env);

})

$('form[data-remote] button#team_button').trigger('click');

start();

})