1 jQuery(function ($) {
\r
2 var csrf_token = $('meta[name=csrf-token]').attr('content'),
\r
3 csrf_param = $('meta[name=csrf-param]').attr('content');
\r
7 * Triggers a custom event on an element and returns the event result
\r
8 * this is used to get around not being able to ensure callbacks are placed
\r
9 * at the end of the chain.
\r
11 * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
\r
12 * own events and placing ourselves at the end of the chain.
\r
14 triggerAndReturn: function (name, data) {
\r
15 var event = new $.Event(name);
\r
16 this.trigger(event, data);
\r
18 return event.result !== false;
\r
22 * Handles execution of remote calls firing overridable events along the way
\r
24 callRemote: function () {
\r
26 method = el.attr('method') || el.attr('data-method') || 'GET',
\r
27 url = el.attr('action') || el.attr('href'),
\r
28 dataType = el.attr('data-type') || 'script';
\r
30 if (url === undefined) {
\r
31 throw "No URL specified for remote call (action or href must be present).";
\r
33 if (el.triggerAndReturn('ajax:before')) {
\r
34 var data = el.is('form') ? el.serializeArray() : [];
\r
39 type: method.toUpperCase(),
\r
40 beforeSend: function (xhr) {
\r
41 el.trigger('ajax:loading', xhr);
\r
43 success: function (data, status, xhr) {
\r
44 el.trigger('ajax:success', [data, status, xhr]);
\r
46 complete: function (xhr) {
\r
47 el.trigger('ajax:complete', xhr);
\r
49 error: function (xhr, status, error) {
\r
50 el.trigger('ajax:failure', [xhr, status, error]);
\r
55 el.trigger('ajax:after');
\r
61 * confirmation handler
\r
63 $('a[data-confirm],input[data-confirm]').live('click', function () {
\r
65 if (el.triggerAndReturn('confirm')) {
\r
66 if (!confirm(el.attr('data-confirm'))) {
\r
76 $('form[data-remote]').live('submit', function (e) {
\r
77 $(this).callRemote();
\r
81 $('a[data-remote],input[data-remote]').live('click', function (e) {
\r
82 $(this).callRemote();
\r
86 $('a[data-method]:not([data-remote])').live('click', function (e){
\r
88 href = link.attr('href'),
\r
89 method = link.attr('data-method'),
\r
90 form = $('<form method="post" action="'+href+'"></form>'),
\r
91 metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';
\r
93 if (csrf_param != null && csrf_token != null) {
\r
94 metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';
\r
98 .append(metadata_input)
\r
101 e.preventDefault();
\r
106 * disable-with handlers
\r
108 var disable_with_input_selector = 'input[data-disable-with]';
\r
109 var disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')';
\r
110 var disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';
\r
112 var disable_with_input_function = function () {
\r
113 $(this).find(disable_with_input_selector).each(function () {
\r
114 var input = $(this);
\r
115 input.data('enable-with', input.val())
\r
116 .attr('value', input.attr('data-disable-with'))
\r
117 .attr('disabled', 'disabled');
\r
121 $(disable_with_form_remote_selector).live('ajax:before', disable_with_input_function);
\r
122 $(disable_with_form_not_remote_selector).live('submit', disable_with_input_function);
\r
124 $(disable_with_form_remote_selector).live('ajax:complete', function () {
\r
125 $(this).find(disable_with_input_selector).each(function () {
\r
126 var input = $(this);
\r
127 input.removeAttr('disabled')
\r
128 .val(input.data('enable-with'));
\r