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
|
jQuery(function ($) {
function handleRemote (e) {
var el = $(this),
data = [],
condition = el.attr('data-condition') ? eval(el.attr('data-condition')) : true,
method = el.attr('method') || el.attr('data-method') || 'POST',
url = el.attr('action') || el.attr('data-url') || '#',
async = el.attr('data-remote-type') === 'synchronous' ? false : true,
update = el.attr('data-update-success'),
position = el.attr('data-update-position');
console.log(e);
if (el.attr('data-submit')) {
data = $('#' + el.attr('data-submit')).serializeArray();
} else if (el.attr('data-with')) {
data = el.attr('data-with');
} else if(e.target.tagName.toUpperCase() == 'FORM') {
data = el.serializeArray();
}
if(condition) {
el.trigger('before');
$.ajax({
async: async,
url: url,
data: data,
type: method.toUpperCase(),
beforeSend: function (xhr) {
el.trigger('after', xhr);
el.trigger('loading', xhr);
},
success: function (data, status, xhr) {
el.trigger('success', [data, status, xhr]);
if (update) {
var element = update.charAt(0) == '#' ? update : '#' + update;
if(position) {
switch(el.attr('data-update-position')) {
case "before":
$(element).before(data);
break;
case "after":
$(element).after(data);
break;
case "top":
$(element).prepend(data);
break;
case "bottom":
$(element).append(data);
break;
default:
$(element).append(data);
break;
}
} else {
$(element).html(data);
}
}
},
complete: function (xhr) {
el.trigger('complete', xhr);
el.trigger('loaded', xhr);
},
error: function (xhr, status, error) {
el.trigger('failure', [xhr, status, error]);
}
});
}
e.preventDefault();
}
$('form[data-remote="true"]').live('submit', handleRemote);
$('a[data-remote="true"],input[data-remote="true"]').live('click', handleRemote);
});
|