最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Execute JavaScript before and after the f:ajax listener is invoked - Stack Overflow

programmeradmin3浏览0评论

It there an easy way to invoke a JavaScript action before and after the invocation of an <f:ajax listener>, e.g. I'd like to invoke window.alert("pre") before and window.alert("post") after onChange is invoked in the backing bean ACtrl:

<h:form>
    <h:inputText id="anId" value="#{cityCtrl.dbHost}">
        <f:ajax event="change" listener="#{aCtrl.onChange}" execute="@all"/>
    </h:inputText>
</h:form>
@ManagedBean
public class ACtrlimplements Serializable {
    public void onChange(AjaxBehaviorEvent event) {
        System.out.println("something changed");
    }
}

Adding multiple f:ajax elements doesn't seem to work (maybe it should?!), e.g. in

<h:form>
    <h:inputText id="anId" value="#{cityCtrl.dbHost}">
        <f:ajax event="change" listener="#{aCtrl.toggle}" execute="@all"/>
        <f:ajax event="change" listener="#{aCtrl.onChange}" execute="@all"/>
        <f:ajax event="change" listener="#{aCtrl.toggle}" execute="@all"/>
    </h:inputText>
</h:form>
@ManagedBean
public class ACtrlimplements Serializable {
    public void onChange(AjaxBehaviorEvent event) {
        System.out.println("something changed");
    }

    public void toggle(AjaxBehaviorEvent event) {
        System.out.println("blah");
    }
}

only ACtrl.onChange is invoked.

It there an easy way to invoke a JavaScript action before and after the invocation of an <f:ajax listener>, e.g. I'd like to invoke window.alert("pre") before and window.alert("post") after onChange is invoked in the backing bean ACtrl:

<h:form>
    <h:inputText id="anId" value="#{cityCtrl.dbHost}">
        <f:ajax event="change" listener="#{aCtrl.onChange}" execute="@all"/>
    </h:inputText>
</h:form>
@ManagedBean
public class ACtrlimplements Serializable {
    public void onChange(AjaxBehaviorEvent event) {
        System.out.println("something changed");
    }
}

Adding multiple f:ajax elements doesn't seem to work (maybe it should?!), e.g. in

<h:form>
    <h:inputText id="anId" value="#{cityCtrl.dbHost}">
        <f:ajax event="change" listener="#{aCtrl.toggle}" execute="@all"/>
        <f:ajax event="change" listener="#{aCtrl.onChange}" execute="@all"/>
        <f:ajax event="change" listener="#{aCtrl.toggle}" execute="@all"/>
    </h:inputText>
</h:form>
@ManagedBean
public class ACtrlimplements Serializable {
    public void onChange(AjaxBehaviorEvent event) {
        System.out.println("something changed");
    }

    public void toggle(AjaxBehaviorEvent event) {
        System.out.println("blah");
    }
}

only ACtrl.onChange is invoked.

Share Improve this question edited Jul 24, 2014 at 6:48 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Jul 23, 2014 at 13:51 Kalle RichterKalle Richter 8,72829 gold badges91 silver badges203 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

Use onevent attribute. It must point to a callback function reference (so don't include parentheses!):

<f:ajax ... onevent="functionName" />

Whereby the actual callback function look like this (JSF will provide the argument all by itself):

function functionName(data) {
    var status = data.status; // Can be "begin", "complete" or "success".
    var source = data.source; // The parent HTML DOM element.

    switch (status) {
        case "begin": // Before the ajax request is sent.
            // ...
            break;

        case "complete": // After the ajax response is arrived.
            // ...
            break;

        case "success": // After update of HTML DOM based on ajax response.
            // ...
            break;
    }
}

See also:

  • JSF 2.0 specification - tables 14.3 and 14.4
  • Is there a way to disable the command link until the ajax response is rendered
  • How to do double-click prevention in JSF 2
发布评论

评论列表(0)

  1. 暂无评论