Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
5af31f37
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,发现更多精彩内容 >>
提交
5af31f37
编写于
2月 05, 2011
作者:
S
Santiago Pastorino
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update jQuery UJS
上级
8b5dc9ca
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
143 addition
and
149 deletion
+143
-149
railties/lib/rails/generators/rails/app/templates/public/javascripts/jquery_ujs.js
...tors/rails/app/templates/public/javascripts/jquery_ujs.js
+143
-149
未找到文件。
railties/lib/rails/generators/rails/app/templates/public/javascripts/jquery_ujs.js
浏览文件 @
5af31f37
/*
* jquery-ujs
*
* http://github.com/rails/jquery-ujs/blob/master/src/rails.js
*
* This rails.js file supports jQuery 1.4.3 and 1.4.4 .
/**
* Unobtrusive scripting adapter for jQuery
*
* Requires jQuery 1.4.3 or later.
* https://github.com/rails/jquery-ujs
*/
jQuery
(
function
(
$
)
{
var
csrf_token
=
$
(
'
meta[name=csrf-token]
'
).
attr
(
'
content
'
),
csrf_param
=
$
(
'
meta[name=csrf-param]
'
).
attr
(
'
content
'
);
$
.
fn
.
extend
({
/**
* Triggers a custom event on an element and returns the event result
* this is used to get around not being able to ensure callbacks are placed
* at the end of the chain.
*/
triggerAndReturn
:
function
(
name
,
data
)
{
var
event
=
new
$
.
Event
(
name
);
this
.
trigger
(
event
,
data
);
return
event
.
result
!==
false
;
},
/**
* Handles execution of remote calls. Provides following callbacks:
*
* - ajax:beforeSend - is executed before firing ajax call
* - ajax:success - is executed when status is success
* - ajax:complete - is executed when the request finishes, whether in failure or success
* - ajax:error - is execute in case of error
*/
callRemote
:
function
()
{
var
el
=
this
,
method
=
el
.
attr
(
'
method
'
)
||
el
.
attr
(
'
data-method
'
)
||
'
GET
'
,
url
=
el
.
attr
(
'
action
'
)
||
el
.
attr
(
'
href
'
),
dataType
=
el
.
attr
(
'
data-type
'
)
||
(
$
.
ajaxSettings
&&
$
.
ajaxSettings
.
dataType
);
if
(
url
===
undefined
)
{
throw
"
No URL specified for remote call (action or href must be present).
"
;
}
else
{
var
$this
=
$
(
this
),
data
=
el
.
is
(
'
form
'
)
?
el
.
serializeArray
()
:
[];
$
.
ajax
({
url
:
url
,
data
:
data
,
dataType
:
dataType
,
type
:
method
.
toUpperCase
(),
beforeSend
:
function
(
xhr
)
{
if
(
$this
.
triggerHandler
(
'
ajax:beforeSend
'
)
===
false
)
{
return
false
;
}
},
success
:
function
(
data
,
status
,
xhr
)
{
el
.
trigger
(
'
ajax:success
'
,
[
data
,
status
,
xhr
]);
},
complete
:
function
(
xhr
)
{
el
.
trigger
(
'
ajax:complete
'
,
xhr
);
},
error
:
function
(
xhr
,
status
,
error
)
{
el
.
trigger
(
'
ajax:error
'
,
[
xhr
,
status
,
error
]);
}
});
}
}
});
/**
* confirmation handler
*/
$
(
'
body
'
).
delegate
(
'
a[data-confirm], button[data-confirm], input[data-confirm]
'
,
'
click.rails
'
,
function
()
{
var
el
=
$
(
this
);
if
(
el
.
triggerAndReturn
(
'
confirm
'
))
{
if
(
!
confirm
(
el
.
attr
(
'
data-confirm
'
)))
{
return
false
;
}
}
});
/**
* remote handlers
*/
$
(
'
form[data-remote]
'
).
live
(
'
submit.rails
'
,
function
(
e
)
{
$
(
this
).
callRemote
();
e
.
preventDefault
();
});
$
(
'
a[data-remote],input[data-remote]
'
).
live
(
'
click.rails
'
,
function
(
e
)
{
$
(
this
).
callRemote
();
e
.
preventDefault
();
});
/**
* <%= link_to "Delete", user_path(@user), :method => :delete, :confirm => "Are you sure?" %>
*
* <a href="/users/5" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>
*/
$
(
'
a[data-method]:not([data-remote])
'
).
live
(
'
click.rails
'
,
function
(
e
){
var
link
=
$
(
this
),
href
=
link
.
attr
(
'
href
'
),
method
=
link
.
attr
(
'
data-method
'
),
form
=
$
(
'
<form method="post" action="
'
+
href
+
'
"></form>
'
),
metadata_input
=
'
<input name="_method" value="
'
+
method
+
'
" type="hidden" />
'
;
if
(
csrf_param
!==
undefined
&&
csrf_token
!==
undefined
)
{
metadata_input
+=
'
<input name="
'
+
csrf_param
+
'
" value="
'
+
csrf_token
+
'
" type="hidden" />
'
;
}
form
.
hide
()
.
append
(
metadata_input
)
.
appendTo
(
'
body
'
);
e
.
preventDefault
();
form
.
submit
();
});
/**
* disable-with handlers
*/
var
disable_with_input_selector
=
'
input[data-disable-with]
'
,
disable_with_form_remote_selector
=
'
form[data-remote]:has(
'
+
disable_with_input_selector
+
'
)
'
,
disable_with_form_not_remote_selector
=
'
form:not([data-remote]):has(
'
+
disable_with_input_selector
+
'
)
'
;
var
disable_with_input_function
=
function
()
{
$
(
this
).
find
(
disable_with_input_selector
).
each
(
function
()
{
var
input
=
$
(
this
);
input
.
data
(
'
enable-with
'
,
input
.
val
())
.
attr
(
'
value
'
,
input
.
attr
(
'
data-disable-with
'
))
.
attr
(
'
disabled
'
,
'
disabled
'
);
});
};
$
(
disable_with_form_remote_selector
).
live
(
'
ajax:before.rails
'
,
disable_with_input_function
);
$
(
disable_with_form_not_remote_selector
).
live
(
'
submit.rails
'
,
disable_with_input_function
);
$
(
disable_with_form_remote_selector
).
live
(
'
ajax:complete.rails
'
,
function
()
{
$
(
this
).
find
(
disable_with_input_selector
).
each
(
function
()
{
var
input
=
$
(
this
);
input
.
removeAttr
(
'
disabled
'
)
.
val
(
input
.
data
(
'
enable-with
'
));
});
});
var
jqueryVersion
=
$
().
jquery
;
if
(
!
(
(
jqueryVersion
===
'
1.4.3
'
)
||
(
jqueryVersion
===
'
1.4.4
'
))){
alert
(
'
This rails.js does not support the jQuery version you are using. Please read documentation.
'
);
(
function
(
$
)
{
// Triggers an event on an element and returns the event result
function
fire
(
obj
,
name
,
data
)
{
var
event
=
new
$
.
Event
(
name
);
obj
.
trigger
(
event
,
data
);
return
event
.
result
!==
false
;
}
// Submits "remote" forms and links with ajax
function
handleRemote
(
element
)
{
var
method
,
url
,
data
,
dataType
=
element
.
attr
(
'
data-type
'
)
||
(
$
.
ajaxSettings
&&
$
.
ajaxSettings
.
dataType
);
if
(
element
.
is
(
'
form
'
))
{
method
=
element
.
attr
(
'
method
'
);
url
=
element
.
attr
(
'
action
'
);
data
=
element
.
serializeArray
();
// memoized value from clicked submit button
var
button
=
element
.
data
(
'
ujs:submit-button
'
);
if
(
button
)
{
data
.
push
(
button
);
element
.
data
(
'
ujs:submit-button
'
,
null
);
}
}
else
{
method
=
element
.
attr
(
'
data-method
'
);
url
=
element
.
attr
(
'
href
'
);
data
=
null
;
}
$
.
ajax
({
url
:
url
,
type
:
method
||
'
GET
'
,
data
:
data
,
dataType
:
dataType
,
// stopping the "ajax:beforeSend" event will cancel the ajax request
beforeSend
:
function
(
xhr
,
settings
)
{
if
(
settings
.
dataType
===
undefined
)
{
xhr
.
setRequestHeader
(
'
accept
'
,
'
*/*;q=0.5,
'
+
settings
.
accepts
.
script
);
}
return
fire
(
element
,
'
ajax:beforeSend
'
,
[
xhr
,
settings
]);
},
success
:
function
(
data
,
status
,
xhr
)
{
element
.
trigger
(
'
ajax:success
'
,
[
data
,
status
,
xhr
]);
},
complete
:
function
(
xhr
,
status
)
{
element
.
trigger
(
'
ajax:complete
'
,
[
xhr
,
status
]);
},
error
:
function
(
xhr
,
status
,
error
)
{
element
.
trigger
(
'
ajax:error
'
,
[
xhr
,
status
,
error
]);
}
});
}
// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
function
handleMethod
(
link
)
{
var
href
=
link
.
attr
(
'
href
'
),
method
=
link
.
attr
(
'
data-method
'
),
csrf_token
=
$
(
'
meta[name=csrf-token]
'
).
attr
(
'
content
'
),
csrf_param
=
$
(
'
meta[name=csrf-param]
'
).
attr
(
'
content
'
),
form
=
$
(
'
<form method="post" action="
'
+
href
+
'
"></form>
'
),
metadata_input
=
'
<input name="_method" value="
'
+
method
+
'
" type="hidden" />
'
;
if
(
csrf_param
!==
undefined
&&
csrf_token
!==
undefined
)
{
metadata_input
+=
'
<input name="
'
+
csrf_param
+
'
" value="
'
+
csrf_token
+
'
" type="hidden" />
'
;
}
form
.
hide
().
append
(
metadata_input
).
appendTo
(
'
body
'
);
form
.
submit
();
}
function
disableFormElements
(
form
)
{
form
.
find
(
'
input[data-disable-with]
'
).
each
(
function
()
{
var
input
=
$
(
this
);
input
.
data
(
'
ujs:enable-with
'
,
input
.
val
())
.
val
(
input
.
attr
(
'
data-disable-with
'
))
.
attr
(
'
disabled
'
,
'
disabled
'
);
});
}
function
enableFormElements
(
form
)
{
form
.
find
(
'
input[data-disable-with]
'
).
each
(
function
()
{
var
input
=
$
(
this
);
input
.
val
(
input
.
data
(
'
ujs:enable-with
'
)).
removeAttr
(
'
disabled
'
);
});
}
function
allowAction
(
element
)
{
var
message
=
element
.
attr
(
'
data-confirm
'
);
return
!
message
||
(
fire
(
element
,
'
confirm
'
)
&&
confirm
(
message
));
}
function
requiredValuesMissing
(
form
)
{
var
missing
=
false
;
form
.
find
(
'
input[name][required]
'
).
each
(
function
()
{
if
(
!
$
(
this
).
val
())
missing
=
true
;
});
return
missing
;
}
});
$
(
'
a[data-confirm], a[data-method], a[data-remote]
'
).
live
(
'
click.rails
'
,
function
(
e
)
{
var
link
=
$
(
this
);
if
(
!
allowAction
(
link
))
return
false
;
if
(
link
.
attr
(
'
data-remote
'
)
!=
undefined
)
{
handleRemote
(
link
);
return
false
;
}
else
if
(
link
.
attr
(
'
data-method
'
))
{
handleMethod
(
link
);
return
false
;
}
});
$
(
'
form
'
).
live
(
'
submit.rails
'
,
function
(
e
)
{
var
form
=
$
(
this
),
remote
=
form
.
attr
(
'
data-remote
'
)
!=
undefined
;
if
(
!
allowAction
(
form
))
return
false
;
// skip other logic when required values are missing
if
(
requiredValuesMissing
(
form
))
return
!
remote
;
if
(
remote
)
{
handleRemote
(
form
);
return
false
;
}
else
{
disableFormElements
(
form
);
}
});
$
(
'
form input[type=submit], form button[type=submit], form button:not([type])
'
).
live
(
'
click.rails
'
,
function
()
{
var
button
=
$
(
this
);
if
(
!
allowAction
(
button
))
return
false
;
// register the pressed submit button
var
name
=
button
.
attr
(
'
name
'
),
data
=
name
?
{
name
:
name
,
value
:
button
.
val
()}
:
null
;
button
.
closest
(
'
form
'
).
data
(
'
ujs:submit-button
'
,
data
);
});
$
(
'
form
'
).
live
(
'
ajax:beforeSend.rails
'
,
function
(
event
)
{
if
(
this
==
event
.
target
)
disableFormElements
(
$
(
this
));
});
$
(
'
form
'
).
live
(
'
ajax:complete.rails
'
,
function
(
event
)
{
if
(
this
==
event
.
target
)
enableFormElements
(
$
(
this
));
});
})(
jQuery
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录