|
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 |
7 | 3 | * |
| 4 | + * Requires jQuery 1.4.3 or later. |
| 5 | + * https://github.com/rails/jquery-ujs |
8 | 6 | */ |
9 | 7 |
|
10 | 8 | (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 | + } |
52 | 30 |
|
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]); |
69 | 45 | } |
70 | | - } |
71 | | - }); |
| 46 | + }); |
| 47 | + } |
72 | 48 |
|
73 | 49 | /** |
74 | 50 | * confirmation handler |
75 | 51 | */ |
76 | 52 | $('a[data-confirm], button[data-confirm], input[data-confirm]').live('click.rails', function() { |
77 | 53 | var el = $(this); |
78 | | - if (el.triggerAndReturn('confirm')) { |
| 54 | + if (fire(el, 'confirm')) { |
79 | 55 | if (!confirm(el.attr('data-confirm'))) { |
80 | 56 | return false; |
81 | 57 | } |
|
86 | 62 | * remote handlers |
87 | 63 | */ |
88 | 64 | $('form[data-remote]').live('submit.rails', function(e) { |
89 | | - $(this).callRemote(); |
| 65 | + handleRemote($(this)); |
90 | 66 | e.preventDefault(); |
91 | 67 | }); |
92 | 68 |
|
93 | 69 | $('a[data-remote],input[data-remote]').live('click.rails', function(e) { |
94 | | - $(this).callRemote(); |
| 70 | + handleRemote($(this)); |
95 | 71 | e.preventDefault(); |
96 | 72 | }); |
97 | 73 |
|
|
0 commit comments