Skip to content

Commit c592fb7

Browse files
committed
refactor handling of "remote" forms and links
- forms now only respect "method" and not "data-method" - jQuery.fn is not augmented with extra methods anymore - "ajax:beforeSend" now bubbles and can abort the ajax request
1 parent 498b35e commit c592fb7

File tree

1 file changed

+44
-68
lines changed

1 file changed

+44
-68
lines changed

src/rails.js

Lines changed: 44 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,57 @@
1-
/*
2-
* jquery-ujs
3-
*
4-
* http://github.com/rails/jquery-ujs/blob/master/src/rails.js
5-
*
6-
* This rails.js file supports jQuery 1.4.3 and 1.4.4 .
1+
/**
2+
* Unobtrusive scripting adapter for jQuery
73
*
4+
* Requires jQuery 1.4.3 or later.
5+
* https://github.com/rails/jquery-ujs
86
*/
97

108
(function($) {
11-
$.fn.extend({
12-
/**
13-
* Triggers a custom event on an element and returns the event result
14-
* this is used to get around not being able to ensure callbacks are placed
15-
* at the end of the chain.
16-
*/
17-
triggerAndReturn: function(name, data) {
18-
var event = new $.Event(name);
19-
this.trigger(event, data);
20-
21-
return event.result !== false;
22-
},
23-
24-
/**
25-
* Handles execution of remote calls. Provides following callbacks:
26-
*
27-
* - ajax:beforeSend - is executed before firing ajax call
28-
* - ajax:success - is executed when status is success
29-
* - ajax:complete - is executed when the request finishes, whether in failure or success
30-
* - ajax:error - is execute in case of error
31-
*/
32-
callRemote: function() {
33-
var el = this,
34-
method = el.attr('method') || el.attr('data-method') || 'GET',
35-
url = el.attr('action') || el.attr('href'),
36-
dataType = el.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
37-
38-
if (url === undefined) {
39-
throw "No URL specified for remote call (action or href must be present).";
40-
} else {
41-
var $this = $(this), data = el.is('form') ? el.serializeArray() : [];
42-
43-
$.ajax({
44-
url: url,
45-
data: data,
46-
dataType: dataType,
47-
type: method.toUpperCase(),
48-
beforeSend: function(xhr) {
49-
if ($this.triggerHandler('ajax:beforeSend') === false) {
50-
return false;
51-
}
9+
// Triggers an event on an element and returns the event result
10+
function fire(obj, name, data) {
11+
var event = new $.Event(name);
12+
obj.trigger(event, data);
13+
return event.result !== false;
14+
}
15+
16+
// Submits "remote" forms and links with ajax
17+
function handleRemote(element) {
18+
var method, url, data,
19+
dataType = element.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
20+
21+
if (element.is('form')) {
22+
method = element.attr('method') || 'POST';
23+
url = element.attr('action');
24+
data = element.serializeArray();
25+
} else {
26+
method = element.attr('data-method') || 'GET';
27+
url = element.attr('href');
28+
data = null;
29+
}
5230

53-
// if user has used jQuery.ajaxSetup then call beforeSend callback
54-
var beforeSendGlobalCallback = $.ajaxSettings && $.ajaxSettings.beforeSend;
55-
if (beforeSendGlobalCallback !== undefined) {
56-
beforeSendGlobalCallback(xhr);
57-
}
58-
},
59-
success: function(data, status, xhr) {
60-
el.trigger('ajax:success', [data, status, xhr]);
61-
},
62-
complete: function(xhr) {
63-
el.trigger('ajax:complete', xhr);
64-
},
65-
error: function(xhr, status, error) {
66-
el.trigger('ajax:error', [xhr, status, error]);
67-
}
68-
});
31+
$.ajax({
32+
url: url, type: method, data: data, dataType: dataType,
33+
// stopping the "ajax:beforeSend" event will cancel the ajax request
34+
beforeSend: function(xhr) {
35+
return fire(element, 'ajax:beforeSend', xhr);
36+
},
37+
success: function(data, status, xhr) {
38+
element.trigger('ajax:success', [data, status, xhr]);
39+
},
40+
complete: function(xhr) {
41+
element.trigger('ajax:complete', xhr);
42+
},
43+
error: function(xhr, status, error) {
44+
element.trigger('ajax:error', [xhr, status, error]);
6945
}
70-
}
71-
});
46+
});
47+
}
7248

7349
/**
7450
* confirmation handler
7551
*/
7652
$('a[data-confirm], button[data-confirm], input[data-confirm]').live('click.rails', function() {
7753
var el = $(this);
78-
if (el.triggerAndReturn('confirm')) {
54+
if (fire(el, 'confirm')) {
7955
if (!confirm(el.attr('data-confirm'))) {
8056
return false;
8157
}
@@ -86,12 +62,12 @@
8662
* remote handlers
8763
*/
8864
$('form[data-remote]').live('submit.rails', function(e) {
89-
$(this).callRemote();
65+
handleRemote($(this));
9066
e.preventDefault();
9167
});
9268

9369
$('a[data-remote],input[data-remote]').live('click.rails', function(e) {
94-
$(this).callRemote();
70+
handleRemote($(this));
9571
e.preventDefault();
9672
});
9773

0 commit comments

Comments
 (0)