forked from rails/jquery-ujs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrails.js
More file actions
121 lines (106 loc) · 4.11 KB
/
rails.js
File metadata and controls
121 lines (106 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
jQuery(function ($) {
var csrf_token = $('meta[name=csrf-token]').attr('content'),
csrf_param = $('meta[name=csrf-param]').attr('content');
$.fn.extend({
/**
* Triggers a custom event on an element and returns the event result
* this is used to get around not being able to ensure callbacks are placed
* at the end of the chain.
*
* TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
* own events and placing ourselves at the end of the chain.
*/
triggerAndReturn: function (name, data) {
var event = new jQuery.Event(name);
this.trigger(event, data);
return event.result !== false;
},
/**
* Handles execution of remote calls firing overridable events along the way
*/
callRemote: function () {
var el = this,
data = el.is('form') ? el.serializeArray() : [],
method = el.attr('method') || el.attr('data-method') || 'GET',
url = el.attr('action') || el.attr('href');
// TODO: should let the developer know no url was found
if (url !== undefined) {
if (el.triggerAndReturn('ajax:before')) {
$.ajax({
url: url,
data: data,
type: method.toUpperCase(),
beforeSend: function (xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
el.trigger('ajax:loading', xhr);
},
success: function (data, status, xhr) {
el.trigger('ajax:success', [data, status, xhr]);
},
complete: function (xhr) {
el.trigger('ajax:complete', xhr);
},
error: function (xhr, status, error) {
el.trigger('ajax:failure', [xhr, status, error]);
}
});
}
el.trigger('ajax:after');
}
}
});
/**
* confirmation handler
*/
$('a[data-confirm],input[data-confirm]').live('click', function () {
var el = $(this);
if (el.triggerAndReturn('confirm')) {
if (!confirm(el.attr('data-confirm'))) {
return false;
}
}
});
/**
* remote handlers
*/
$('form[data-remote="true"]').live('submit', function (e) {
$(this).callRemote();
e.preventDefault();
});
$('a[data-remote="true"],input[data-remote="true"]').live('click', function (e) {
$(this).callRemote();
e.preventDefault();
});
$('a[data-method][data-remote!=true]').live('click',function(e){
var link = $(this),
href = link.attr('href'),
method = link.attr('data-method'),
form = $('<form method="post" action="'+href+'">'),
input = $('<input name="_method" value="'+method+'" type="hidden" />'),
csrf_input = $('<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />');
form.hide()
.append(input)
.append(csrf_input)
.appendTo('body'); // redundant?
e.preventDefault();
form.submit();
});
/**
* disable_with handlers
*/
$('form[data-remote="true"]').live('ajax:before', function () {
$(this).children('input[data-disable-with]').each(function () {
var input = $(this);
input.data('enable_with', input.val())
.attr('value', input.attr('data-disable-with'))
.attr('disabled', 'disabled');
});
});
$('form[data-remote="true"]').live('ajax:after', function () {
$(this).children('input[data-disable-with]').each(function () {
var input = $(this);
input.removeAttr('disabled')
.val(input.data('enable_with'));
});
});
});