2

Is there any way to trigger an event in my composite component with clientBehavior and jQuery?

If not possible, what is the proper way to create custom events in composites?

Example:

ajaxTest.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:cc="http://xmlns.jcp.org/jsf/composite"
      xmlns:p="http://primefaces.org/ui">

    <cc:interface>
        <cc:clientBehavior name="myEvent" targets="myLabel" event="myEvent"/>
    </cc:interface>

    <cc:implementation>
        <div id="#{cc.clientId}">
            <p:outputLabel id="myLabel" value="Test label" />
            <p:commandLink value="trigger event"
                           onstart="$(PrimeFaces.escapeClientId('#{cc.clientId}:myLabel')).trigger('myEvent');"/>            
        </div>
    </cc:implementation>
</html>

testPage.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui"
        xmlns:demo="http://xmlns.jcp.org/jsf/composite/component">

<h:body>
        <h:form id="form-1">
            <demo:ajaxTest>
                <p:ajax event="myEvent" oncomplete="alert('Ajax called');" />
            </demo:ajaxTest>
        </h:form>
</h:body>
</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Plutoz
  • 694
  • 9
  • 13

1 Answers1

2

The <cc:clientBehavior event> must declare a valid event name as supported by the target component in question. In this context, it must thus be exactly the same event name as you would use when nesting <p:ajax> directly inside <p:commandLink>. You perhaps want action or click.

<cc:clientBehavior ... event="action" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You're right, in the current context this is not a jQuery problem, so I may oversimplified it. I am trying to integrate a jQuery addon (ShapeShift 2) to my composite, and I don't have any idea, how can I trigger ajax events from jQuery .on() callbacks. Is this problem worth another question? – Plutoz Sep 24 '14 at 11:25
  • You could use ``. See also http://stackoverflow.com/questions/16588327/how-to-invoke-a-jsf-managed-bean-on-a-html-dom-event-using-native-javascript/ – BalusC Sep 24 '14 at 12:53