|
46 | 46 | }); |
47 | 47 | } |
48 | 48 |
|
49 | | - /** |
50 | | - * confirmation handler |
51 | | - */ |
52 | | - $('a[data-confirm], button[data-confirm], input[data-confirm]').live('click.rails', function() { |
53 | | - var el = $(this); |
54 | | - if (fire(el, 'confirm')) { |
55 | | - if (!confirm(el.attr('data-confirm'))) { |
56 | | - return false; |
57 | | - } |
58 | | - } |
59 | | - }); |
60 | | - |
61 | | - /** |
62 | | - * remote handlers |
63 | | - */ |
64 | | - $('form[data-remote]').live('submit.rails', function(e) { |
65 | | - handleRemote($(this)); |
66 | | - e.preventDefault(); |
67 | | - }); |
68 | | - |
69 | | - $('a[data-remote],input[data-remote]').live('click.rails', function(e) { |
70 | | - handleRemote($(this)); |
71 | | - e.preventDefault(); |
72 | | - }); |
73 | | - |
74 | | - /** |
75 | | - * <%= link_to "Delete", user_path(@user), :method => :delete, :confirm => "Are you sure?" %> |
76 | | - * |
77 | | - * <a href="/users/5" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a> |
78 | | - */ |
79 | | - $('a[data-method]:not([data-remote])').live('click.rails', function(e) { |
80 | | - var link = $(this), |
81 | | - href = link.attr('href'), |
| 49 | + // Handles "data-method" on links such as: |
| 50 | + // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a> |
| 51 | + function handleMethod(link) { |
| 52 | + var href = link.attr('href'), |
82 | 53 | method = link.attr('data-method'), |
83 | 54 | csrf_token = $('meta[name=csrf-token]').attr('content'), |
84 | 55 | csrf_param = $('meta[name=csrf-param]').attr('content'), |
|
90 | 61 | } |
91 | 62 |
|
92 | 63 | form.hide().append(metadata_input).appendTo('body'); |
93 | | - |
94 | | - e.preventDefault(); |
95 | 64 | form.submit(); |
| 65 | + } |
| 66 | + |
| 67 | + function allowAction(element) { |
| 68 | + var message = element.attr('data-confirm'); |
| 69 | + return !message || (fire(element, 'confirm') && confirm(message)); |
| 70 | + } |
| 71 | + |
| 72 | + $('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) { |
| 73 | + var link = $(this); |
| 74 | + if (!allowAction(link)) return false; |
| 75 | + |
| 76 | + if (link.attr('data-remote')) { |
| 77 | + handleRemote(link); |
| 78 | + return false; |
| 79 | + } else if (link.attr('data-method')) { |
| 80 | + handleMethod(link); |
| 81 | + return false; |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + $('form').live('submit.rails', function(e) { |
| 86 | + var form = $(this); |
| 87 | + if (!allowAction(form)) return false; |
| 88 | + |
| 89 | + if (form.attr('data-remote')) { |
| 90 | + handleRemote(form); |
| 91 | + return false; |
| 92 | + } |
96 | 93 | }); |
97 | 94 |
|
98 | 95 | /** |
|
0 commit comments