Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3812ef8b
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
3812ef8b
编写于
4月 19, 2018
作者:
R
Rafael França
提交者:
GitHub
4月 19, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #32574 from DmytroVasin/rails-ujs-stoppable-events
Rails-ujs: Info about stoppable events
上级
7a9098fa
e3fcd96d
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
73 addition
and
64 deletion
+73
-64
actionview/test/ujs/public/test/call-remote-callbacks.js
actionview/test/ujs/public/test/call-remote-callbacks.js
+6
-6
actionview/test/ujs/public/test/call-remote.js
actionview/test/ujs/public/test/call-remote.js
+4
-4
actionview/test/ujs/public/test/data-confirm.js
actionview/test/ujs/public/test/data-confirm.js
+8
-8
actionview/test/ujs/public/test/data-disable-with.js
actionview/test/ujs/public/test/data-disable-with.js
+15
-12
actionview/test/ujs/public/test/data-disable.js
actionview/test/ujs/public/test/data-disable.js
+13
-12
actionview/test/ujs/public/test/data-remote.js
actionview/test/ujs/public/test/data-remote.js
+12
-11
actionview/test/ujs/public/test/override.js
actionview/test/ujs/public/test/override.js
+2
-2
actionview/test/ujs/public/test/settings.js
actionview/test/ujs/public/test/settings.js
+7
-5
guides/source/working_with_javascript_in_rails.md
guides/source/working_with_javascript_in_rails.md
+6
-4
未找到文件。
actionview/test/ujs/public/test/call-remote-callbacks.js
浏览文件 @
3812ef8b
...
...
@@ -75,9 +75,9 @@ asyncTest('setting data("with-credentials",true) with "ajax:before" uses new set
asyncTest
(
'
stopping the "ajax:beforeSend" event aborts the request
'
,
1
,
function
()
{
submit
(
function
(
form
)
{
form
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
form
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
)
{
ok
(
true
,
'
aborting request in ajax:beforeSend
'
)
return
false
e
.
preventDefault
()
})
form
.
unbind
(
'
ajax:send
'
).
bindNative
(
'
ajax:send
'
,
function
()
{
ok
(
false
,
'
ajax:send should not run
'
)
...
...
@@ -148,8 +148,8 @@ function skipIt() {
.
bind
(
'
iframe:loading
'
,
function
()
{
ok
(
false
,
'
form should not get submitted
'
)
})
.
bindNative
(
'
ajax:aborted:file
'
,
function
()
{
return
false
.
bindNative
(
'
ajax:aborted:file
'
,
function
(
e
)
{
e
.
preventDefault
()
})
.
triggerNative
(
'
submit
'
)
...
...
@@ -162,9 +162,9 @@ function skipIt() {
}
asyncTest
(
'
"ajax:beforeSend" can be observed and stopped with event delegation
'
,
1
,
function
()
{
$
(
document
).
delegate
(
'
form[data-remote]
'
,
'
ajax:beforeSend
'
,
function
()
{
$
(
document
).
delegate
(
'
form[data-remote]
'
,
'
ajax:beforeSend
'
,
function
(
e
)
{
ok
(
true
,
'
ajax:beforeSend observed with event delegation
'
)
return
false
e
.
preventDefault
()
})
submit
(
function
(
form
)
{
...
...
actionview/test/ujs/public/test/call-remote.js
浏览文件 @
3812ef8b
...
...
@@ -210,7 +210,7 @@ asyncTest('allow empty form "action"', 1, function() {
buildForm
({
action
:
''
})
$
(
'
#qunit-fixture
'
).
find
(
'
form
'
)
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
,
xhr
,
settings
)
{
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
vt
,
xhr
,
settings
)
{
// Get current location (the same way jQuery does)
try
{
currentLocation
=
location
.
href
...
...
@@ -229,7 +229,7 @@ asyncTest('allow empty form "action"', 1, function() {
// Prevent the request from actually getting sent to the current page and
// causing an error.
return
false
evt
.
preventDefault
()
})
.
triggerNative
(
'
submit
'
)
...
...
@@ -257,7 +257,7 @@ asyncTest('intelligently guesses crossDomain behavior when target URL has a diff
equal
(
settings
.
crossDomain
,
true
,
'
crossDomain should be set to true
'
)
// prevent request from actually getting sent off-domain
return
false
evt
.
preventDefault
()
})
.
triggerNative
(
'
submit
'
)
...
...
@@ -276,7 +276,7 @@ asyncTest('intelligently guesses crossDomain behavior when target URL consists o
equal
(
settings
.
crossDomain
,
false
,
'
crossDomain should be set to false
'
)
// prevent request from actually getting sent off-domain
return
false
evt
.
preventDefault
()
})
.
triggerNative
(
'
submit
'
)
...
...
actionview/test/ujs/public/test/data-confirm.js
浏览文件 @
3812ef8b
...
...
@@ -173,9 +173,9 @@ asyncTest('binding to confirm event of a link and returning false', 1, function(
}
$
(
'
a[data-confirm]
'
)
.
bindNative
(
'
confirm
'
,
function
()
{
.
bindNative
(
'
confirm
'
,
function
(
e
)
{
App
.
assertCallbackInvoked
(
'
confirm
'
)
return
false
e
.
preventDefault
()
})
.
bindNative
(
'
confirm:complete
'
,
function
()
{
App
.
assertCallbackNotInvoked
(
'
confirm:complete
'
)
...
...
@@ -194,9 +194,9 @@ asyncTest('binding to confirm event of a button and returning false', 1, functio
}
$
(
'
button[data-confirm]
'
)
.
bindNative
(
'
confirm
'
,
function
()
{
.
bindNative
(
'
confirm
'
,
function
(
e
)
{
App
.
assertCallbackInvoked
(
'
confirm
'
)
return
false
e
.
preventDefault
()
})
.
bindNative
(
'
confirm:complete
'
,
function
()
{
App
.
assertCallbackNotInvoked
(
'
confirm:complete
'
)
...
...
@@ -216,9 +216,9 @@ asyncTest('binding to confirm:complete event of a link and returning false', 2,
}
$
(
'
a[data-confirm]
'
)
.
bindNative
(
'
confirm:complete
'
,
function
()
{
.
bindNative
(
'
confirm:complete
'
,
function
(
e
)
{
App
.
assertCallbackInvoked
(
'
confirm:complete
'
)
return
false
e
.
preventDefault
()
})
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
App
.
assertCallbackNotInvoked
(
'
ajax:beforeSend
'
)
...
...
@@ -238,9 +238,9 @@ asyncTest('binding to confirm:complete event of a button and returning false', 2
}
$
(
'
button[data-confirm]
'
)
.
bindNative
(
'
confirm:complete
'
,
function
()
{
.
bindNative
(
'
confirm:complete
'
,
function
(
e
)
{
App
.
assertCallbackInvoked
(
'
confirm:complete
'
)
return
false
e
.
preventDefault
()
})
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
App
.
assertCallbackNotInvoked
(
'
ajax:beforeSend
'
)
...
...
actionview/test/ujs/public/test/data-disable-with.js
浏览文件 @
3812ef8b
...
...
@@ -132,7 +132,8 @@ test('form input[type=submit][data-disable-with] re-enables when `pageshow` even
})
asyncTest
(
'
form[data-remote] input[type=submit][data-disable-with] is replaced in ajax callback
'
,
2
,
function
()
{
var
form
=
$
(
'
form:not([data-remote])
'
).
attr
(
'
data-remote
'
,
'
true
'
),
origFormContents
=
form
.
html
()
var
form
=
$
(
'
#qunit-fixture form:not([data-remote])
'
).
attr
(
'
data-remote
'
,
'
true
'
),
origFormContents
=
form
.
html
()
form
.
bindNative
(
'
ajax:success
'
,
function
()
{
form
.
html
(
origFormContents
)
...
...
@@ -146,7 +147,8 @@ asyncTest('form[data-remote] input[type=submit][data-disable-with] is replaced i
})
asyncTest
(
'
form[data-remote] input[data-disable-with] is replaced with disabled field in ajax callback
'
,
2
,
function
()
{
var
form
=
$
(
'
form:not([data-remote])
'
).
attr
(
'
data-remote
'
,
'
true
'
),
input
=
form
.
find
(
'
input[type=submit]
'
),
var
form
=
$
(
'
#qunit-fixture form:not([data-remote])
'
).
attr
(
'
data-remote
'
,
'
true
'
),
input
=
form
.
find
(
'
input[type=submit]
'
),
newDisabledInput
=
input
.
clone
().
attr
(
'
disabled
'
,
'
disabled
'
)
form
.
bindNative
(
'
ajax:success
'
,
function
()
{
...
...
@@ -238,9 +240,9 @@ asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:before` event
App
.
checkEnabledState
(
link
,
'
Click me
'
)
link
.
bindNative
(
'
ajax:before
'
,
function
()
{
.
bindNative
(
'
ajax:before
'
,
function
(
e
)
{
App
.
checkDisabledState
(
link
,
'
clicking...
'
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
@@ -256,9 +258,9 @@ asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:beforeSend` e
App
.
checkEnabledState
(
link
,
'
Click me
'
)
link
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
)
{
App
.
checkDisabledState
(
link
,
'
clicking...
'
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
@@ -293,8 +295,9 @@ asyncTest('form[data-remote] input|button|textarea[data-disable-with] does not d
submit
=
$
(
'
<input type="submit" data-disable-with="submitting ..." name="submit2" value="Submit" />
'
).
appendTo
(
form
)
form
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
return
false
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
)
{
e
.
preventDefault
()
e
.
stopPropagation
()
})
.
triggerNative
(
'
submit
'
)
...
...
@@ -343,9 +346,9 @@ asyncTest('button[data-remote][data-disable-with] re-enables when `ajax:before`
App
.
checkEnabledState
(
button
,
'
Click me
'
)
button
.
bindNative
(
'
ajax:before
'
,
function
()
{
.
bindNative
(
'
ajax:before
'
,
function
(
e
)
{
App
.
checkDisabledState
(
button
,
'
clicking...
'
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
@@ -361,9 +364,9 @@ asyncTest('button[data-remote][data-disable-with] re-enables when `ajax:beforeSe
App
.
checkEnabledState
(
button
,
'
Click me
'
)
button
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
)
{
App
.
checkDisabledState
(
button
,
'
clicking...
'
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
actionview/test/ujs/public/test/data-disable.js
浏览文件 @
3812ef8b
...
...
@@ -91,7 +91,7 @@ asyncTest('form input[type=submit][data-disable] disables', 6, function() {
})
asyncTest
(
'
form[data-remote] input[type=submit][data-disable] is replaced in ajax callback
'
,
2
,
function
()
{
var
form
=
$
(
'
form:not([data-remote])
'
).
attr
(
'
data-remote
'
,
'
true
'
),
origFormContents
=
form
.
html
()
var
form
=
$
(
'
#qunit-fixture
form:not([data-remote])
'
).
attr
(
'
data-remote
'
,
'
true
'
),
origFormContents
=
form
.
html
()
form
.
bindNative
(
'
ajax:success
'
,
function
()
{
form
.
html
(
origFormContents
)
...
...
@@ -105,7 +105,7 @@ asyncTest('form[data-remote] input[type=submit][data-disable] is replaced in aja
})
asyncTest
(
'
form[data-remote] input[data-disable] is replaced with disabled field in ajax callback
'
,
2
,
function
()
{
var
form
=
$
(
'
form:not([data-remote])
'
).
attr
(
'
data-remote
'
,
'
true
'
),
input
=
form
.
find
(
'
input[type=submit]
'
),
var
form
=
$
(
'
#qunit-fixture
form:not([data-remote])
'
).
attr
(
'
data-remote
'
,
'
true
'
),
input
=
form
.
find
(
'
input[type=submit]
'
),
newDisabledInput
=
input
.
clone
().
attr
(
'
disabled
'
,
'
disabled
'
)
form
.
bindNative
(
'
ajax:success
'
,
function
()
{
...
...
@@ -168,9 +168,9 @@ asyncTest('a[data-remote][data-disable] re-enables when `ajax:before` event is c
App
.
checkEnabledState
(
link
,
'
Click me
'
)
link
.
bindNative
(
'
ajax:before
'
,
function
()
{
.
bindNative
(
'
ajax:before
'
,
function
(
e
)
{
App
.
checkDisabledState
(
link
,
'
Click me
'
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
@@ -186,9 +186,9 @@ asyncTest('a[data-remote][data-disable] re-enables when `ajax:beforeSend` event
App
.
checkEnabledState
(
link
,
'
Click me
'
)
link
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
)
{
App
.
checkDisabledState
(
link
,
'
Click me
'
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
@@ -223,8 +223,9 @@ asyncTest('form[data-remote] input|button|textarea[data-disable] does not disabl
submit
=
$
(
'
<input type="submit" data-disable="submitting ..." name="submit2" value="Submit" />
'
).
appendTo
(
form
)
form
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
return
false
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
)
{
e
.
preventDefault
()
e
.
stopPropagation
()
})
.
triggerNative
(
'
submit
'
)
...
...
@@ -273,9 +274,9 @@ asyncTest('button[data-remote][data-disable] re-enables when `ajax:before` event
App
.
checkEnabledState
(
button
,
'
Click me
'
)
button
.
bindNative
(
'
ajax:before
'
,
function
()
{
.
bindNative
(
'
ajax:before
'
,
function
(
e
)
{
App
.
checkDisabledState
(
button
,
'
Click me
'
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
@@ -291,9 +292,9 @@ asyncTest('button[data-remote][data-disable] re-enables when `ajax:beforeSend` e
App
.
checkEnabledState
(
button
,
'
Click me
'
)
button
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
)
{
App
.
checkDisabledState
(
button
,
'
Click me
'
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
actionview/test/ujs/public/test/data-remote.js
浏览文件 @
3812ef8b
...
...
@@ -272,9 +272,10 @@ asyncTest('returning false in form\'s submit bindings in non-submit-bubbling bro
form
.
append
(
$
(
'
<input type="submit" />
'
))
.
bindNative
(
'
submit
'
,
function
()
{
.
bindNative
(
'
submit
'
,
function
(
e
)
{
ok
(
true
,
'
binding handler is called
'
)
return
false
e
.
preventDefault
()
e
.
stopPropagation
()
})
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
ok
(
false
,
'
form should not be submitted
'
)
...
...
@@ -296,8 +297,8 @@ asyncTest('clicking on a link with falsy "data-remote" attribute does not fire a
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
ok
(
false
,
'
ajax should not be triggered
'
)
})
.
bindNative
(
'
click
'
,
function
()
{
return
false
.
bindNative
(
'
click
'
,
function
(
e
)
{
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
@@ -314,8 +315,8 @@ asyncTest('ctrl-clicking on a link with falsy "data-remote" attribute does not f
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
ok
(
false
,
'
ajax should not be triggered
'
)
})
.
bindNative
(
'
click
'
,
function
()
{
return
false
.
bindNative
(
'
click
'
,
function
(
e
)
{
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
,
{
metaKey
:
true
})
...
...
@@ -333,8 +334,8 @@ asyncTest('clicking on a button with falsy "data-remote" attribute', 0, function
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
ok
(
false
,
'
ajax should not be triggered
'
)
})
.
bindNative
(
'
click
'
,
function
()
{
return
false
.
bindNative
(
'
click
'
,
function
(
e
)
{
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
...
...
@@ -347,8 +348,8 @@ asyncTest('submitting a form with falsy "data-remote" attribute', 0, function()
.
bindNative
(
'
ajax:beforeSend
'
,
function
()
{
ok
(
false
,
'
ajax should not be triggered
'
)
})
.
bindNative
(
'
submit
'
,
function
()
{
return
false
.
bindNative
(
'
submit
'
,
function
(
e
)
{
e
.
preventDefault
()
})
.
triggerNative
(
'
submit
'
)
...
...
@@ -429,7 +430,7 @@ asyncTest('changing a select option without "data-url" attribute still fires aja
ajaxLocation
=
settings
.
url
.
replace
(
settings
.
data
,
''
).
replace
(
/&$/
,
''
).
replace
(
/
\?
$/
,
''
)
equal
(
ajaxLocation
,
currentLocation
,
'
URL should be current page by default
'
)
return
false
e
.
preventDefault
()
})
.
val
(
'
optionValue2
'
)
.
triggerNative
(
'
change
'
)
...
...
actionview/test/ujs/public/test/override.js
浏览文件 @
3812ef8b
...
...
@@ -25,7 +25,7 @@ asyncTest('the getter for an element\'s href is overridable', 1, function() {
$
(
'
#qunit-fixture a
'
)
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
,
xhr
,
options
)
{
equal
(
'
/data/href
'
,
options
.
url
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
start
()
...
...
@@ -35,7 +35,7 @@ asyncTest('the getter for an element\'s href works normally if not overridden',
$
(
'
#qunit-fixture a
'
)
.
bindNative
(
'
ajax:beforeSend
'
,
function
(
e
,
xhr
,
options
)
{
equal
(
location
.
protocol
+
'
//
'
+
location
.
host
+
'
/real/href
'
,
options
.
url
)
return
false
e
.
preventDefault
()
})
.
triggerNative
(
'
click
'
)
start
()
...
...
actionview/test/ujs/public/test/settings.js
浏览文件 @
3812ef8b
...
...
@@ -103,14 +103,16 @@ $.fn.extend({
bindNative
:
function
(
event
,
handler
)
{
if
(
!
handler
)
return
this
this
.
bind
(
event
,
function
(
e
)
{
var
el
=
this
[
0
]
el
.
addEventListener
(
event
,
function
(
e
)
{
var
args
=
[]
if
(
e
.
originalEvent
.
detail
)
{
args
=
e
.
originalEvent
.
detail
.
slice
()
if
(
e
.
detail
)
{
args
=
e
.
detail
.
slice
()
}
args
.
unshift
(
e
)
return
handler
.
apply
(
this
,
args
)
})
return
handler
.
apply
(
el
,
args
)
},
false
)
return
this
}
})
guides/source/working_with_javascript_in_rails.md
浏览文件 @
3812ef8b
...
...
@@ -382,10 +382,9 @@ have been bundled into `event.detail`. For information about the previously used
`jquery-ujs`
in Rails 5 and earlier, read the
[
`jquery-ujs` wiki
](
https://github.com/rails/jquery-ujs/wiki/ajax
)
.
### Stoppable events
If you stop
`ajax:before`
or
`ajax:beforeSend`
by returning false from the
handler method, the Ajax request will never take place. The
`ajax:before`
event
can manipulate form data before serialization and the
You can stop execution of the Ajax request by running
`event.preventDefault()`
from the handlers methods
`ajax:before`
or
`ajax:beforeSend`
.
The
`ajax:before`
event can manipulate form data before serialization and the
`ajax:beforeSend`
event is useful for adding custom request headers.
If you stop the
`ajax:aborted:file`
event, the default behavior of allowing the
...
...
@@ -393,6 +392,9 @@ browser to submit the form via normal means (i.e. non-Ajax submission) will be
canceled and the form will not be submitted at all. This is useful for
implementing your own Ajax file upload workaround.
Note, you should use
`return false`
to prevent event for
`jquery-ujs`
and
`e.preventDefault()`
for
`rails-ujs`
Server-Side Concerns
--------------------
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录