File tree 4 files changed +37
-2
lines changed
app/assets/javascripts/rails-ujs/features
4 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,12 @@ Rails.handleDisabledElement = (e) ->
8
8
9
9
# Unified function to enable an element (link, button and form)
10
10
Rails .enableElement = (e ) ->
11
- element = if e instanceof Event then e .target else e
11
+ if e instanceof Event
12
+ return if isXhrRedirect (e)
13
+ element = e .target
14
+ else
15
+ element = e
16
+
12
17
if matches (element, Rails .linkDisableSelector )
13
18
enableLinkElement (element)
14
19
else if matches (element, Rails .buttonDisableSelector ) or matches (element, Rails .formEnableSelector )
@@ -80,3 +85,7 @@ enableFormElement = (element) ->
80
85
setData (element, ' ujs:enable-with' , null ) # clean up cache
81
86
element .disabled = false
82
87
setData (element, ' ujs:disabled' , null )
88
+
89
+ isXhrRedirect = (event ) ->
90
+ xhr = event .detail ? [0 ]
91
+ xhr ? .getResponseHeader (" X-Xhr-Redirect" )?
Original file line number Diff line number Diff line change @@ -320,3 +320,20 @@ asyncTest('button[data-remote][data-disable] re-enables when `ajax:error` event
320
320
start ( )
321
321
} , 30 )
322
322
} )
323
+
324
+ asyncTest ( 'do not enable elements for XHR redirects' , 6 , function ( ) {
325
+ var link = $ ( 'a[data-disable]' ) . attr ( 'data-remote' , true ) . attr ( 'href' , '/echo?with_xhr_redirect=true' )
326
+
327
+ App . checkEnabledState ( link , 'Click me' )
328
+
329
+ link
330
+ . bindNative ( 'ajax:send' , function ( ) {
331
+ App . checkDisabledState ( link , 'Click me' )
332
+ } )
333
+ . triggerNative ( 'click' )
334
+
335
+ setTimeout ( function ( ) {
336
+ App . checkDisabledState ( link , 'Click me' )
337
+ start ( )
338
+ } , 30 )
339
+ } )
Original file line number Diff line number Diff line change 1
1
var App = App || { }
2
+ var Turbolinks = Turbolinks || { }
2
3
3
4
App . assertCallbackInvoked = function ( callbackName ) {
4
5
ok ( true , callbackName + ' callback should have been invoked' )
@@ -116,3 +117,6 @@ $.fn.extend({
116
117
return this
117
118
}
118
119
} )
120
+
121
+ Turbolinks . clearCache = function ( ) { }
122
+ Turbolinks . visit = function ( ) { }
Original file line number Diff line number Diff line change @@ -64,7 +64,12 @@ def echo
64
64
if params [ :content_type ] && params [ :content ]
65
65
render inline : params [ :content ] , content_type : params [ :content_type ]
66
66
elsif request . xhr?
67
- render json : JSON . generate ( data )
67
+ if params [ :with_xhr_redirect ]
68
+ response . set_header ( "X-Xhr-Redirect" , "http://example.com/" )
69
+ render inline : %{Turbolinks.clearCache()\n Turbolinks.visit("http://example.com/", {"action":"replace"})}
70
+ else
71
+ render json : JSON . generate ( data )
72
+ end
68
73
elsif params [ :iframe ]
69
74
payload = JSON . generate ( data ) . gsub ( "<" , "<" ) . gsub ( ">" , ">" )
70
75
html = <<-HTML
You can’t perform that action at this time.
0 commit comments