map*_*aft 14 javascript ajax jsf jsf-2
我有一个javascript函数,我希望在JSF 2中的每个异步回发后执行.
我已完成以下操作以确保执行每个完整页面的回发:
jQuery(document).ready(mahFunction);Run Code Online (Sandbox Code Playgroud)
我需要这样做的原因是解决第三方JSF组件库中的故障,因此我无法修改服务器呈现阶段中的任何内容来为组件执行此操作.
我很难找到有关这方面的信息,因为我使用了错误的条款.我曾经是一名ASP.NET开发人员,我将这些术语称为"整页回发"和"部分回发",似乎其他JSF开发人员不使用这些术语.
Bal*_*usC 31
您可以通过以下方式将其注册为JSF ajax回调处理程序jsf.ajax.addOnEvent:
<script>
jsf.ajax.addOnEvent(foo);
</script>
Run Code Online (Sandbox Code Playgroud)
同
<script>
function foo(data) {
var ajaxStatus = data.status; // Can be "begin", "complete" and "success".
switch (ajaxStatus) {
case "begin": // Right before sending ajax request.
// ...
break;
case "complete": // Right after receiving ajax response.
// ...
break;
case "success": // When ajax response is successfully processed.
// ...
break;
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
其中data对象具有几个有用的XHR派生属性,如JSF 2.0规范的表14-4中所述(单击For Evaluation链接).这是相关的摘录:
表14-4事件数据有效负载
Name Description/Value ------------- ---------------------------------------------------- type “event” status One of the events specified in TABLE 14-3 source The DOM element that triggered the Ajax request. responseCode Ajax request object ‘status’ (XMLHttpRequest.status); Not present for “begin” event; responseXML The XML response (XMLHttpRequest.responseXML); Not present for “begin” event; responseText The text response (XMLHttpResponse.responseTxt); Not present for “begin” event;
作为替代方案,XHTML声明方式是将脚本调用嵌入JSF组件中,id并让ajax调用重新呈现它.该组件应该依次有条件地呈现脚本FacesContext#isPostback().
<h:form>
<h:commandButton value="Submit" action="#{bean.submit}">
<f:ajax render=":myscript">
</h:commandButton>
</h:form>
<h:panelGroup id="myscript">
<h:outputScript rendered="#{facesContext.postback}">foo();</h:outputScript>
</h:panelGroup>
Run Code Online (Sandbox Code Playgroud)
然而,大多数组件库都有更好的抽象支持.既然你没有提到你正在使用哪一个,那么可以给出更合适的答案,这里只是一个基于PrimeFaces命令按钮的随机例子.
<p:commandButton value="Submit" action="#{bean.submit}" oncomplete="foo();" />
Run Code Online (Sandbox Code Playgroud)
请参阅您正在使用的组件库的文档.
| 归档时间: |
|
| 查看次数: |
17202 次 |
| 最近记录: |