每次 JSF ajax 回发后执行 JavaScript

发布于 2024-11-02 01:21:10 字数 317 浏览 1 评论 0原文

我有一个 javascript 函数,我想在 JSF 2 中的每次异步回发后执行该函数

。我已执行以下操作以确保执行每个整页回发:

jQuery(document).ready(mahFunction);

我需要这样做的原因是为了解决第三个中的故障-party JSF 组件库,因此我无法在服务器渲染阶段修改任何内容来为组件执行此操作。

我无法找到这方面的信息,可能是因为我使用了不正确的术语。我曾经是一名 ASP.NET 开发人员,我将这些术语称为“全页回发”和“部分回发”,而其他 JSF 开发人员似乎不使用这些术语。

I have a javascript function that I would like to execute after every asynchronous postback in JSF 2.

I have done the following to ensure that every full page postback this gets executed:

jQuery(document).ready(mahFunction);

The reason I need to do this is to workaround a glitch in a third-party JSF component library so I cannot modify anything in the server render phase to do this for the component.

I am having trouble finding information on this possibly because I am using the incorrect terms. I used to be an ASP.NET developer and I refer to these terms as "full page postback" and "partial postback" where it seems other JSF developers do not use such terms.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2024-11-09 01:21:10

您可以通过 将其注册为 JSF ajax 回调处理程序jsf.ajax.addOnEvent

<script>
    jsf.ajax.addOnEvent(foo);
</script>

其中

<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>

data 对象具有几个有用的 XHR 派生属性,如 JSF 2.0 规范(单击评估链接) 。以下是相关性的摘录:

表 14-4 事件数据负载

 名称 说明/值
    ------------- ------------------------------------- ----------------
    输入“事件”
    status 表 14-3 中指定的事件之一
    source 触发 Ajax 请求的 DOM 元素。
    responseCode Ajax 请求对象 'status' (XMLHttpRequest.status); 
                   未出席“开始”活动;
    responseXML XML 响应 (XMLHttpRequest.responseXML); 
                   未出席“开始”活动;
    responseText 文本响应 (XMLHttpResponse.responseTxt);
                   未出席“开始”活动;


作为替代方案,XHTML 声明式方法是将脚本调用嵌入到具有 id 的 JSF 组件中,并让 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>

然而,大多数组件库对此都有更好的抽象支持。由于您没有提及您正在使用哪一个,以便可以给出更合适的答案,因此这里只是一个基于 PrimeFaces< 的随机示例/a> 命令按钮。

<p:commandButton value="Submit" action="#{bean.submit}" oncomplete="foo();" />

请参阅您正在使用的组件库的文档。

另请参阅:

You could register it as JSF ajax callback handler by jsf.ajax.addOnEvent:

<script>
    jsf.ajax.addOnEvent(foo);
</script>

with

<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>

where the data object has several useful XHR derived properties which is described in table 14-4 of JSF 2.0 specification (click the For Evaluation link). Here's an extract of relevance:

TABLE 14-4 Event Data Payload

    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;

As an alternative, a XHTML-declarative way would be to just embed the script call in a JSF component with an id and let the ajax call re-render it. The component should in turn render the script conditionally based on 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>

Most component libraries have however better abstracted support for this. Since you didn't mention which one you're using so that a more suited answer can be given, here's just a random example based on PrimeFaces command button.

<p:commandButton value="Submit" action="#{bean.submit}" oncomplete="foo();" />

Refer the documentation of the component library you're using.

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文