JSF - How to pass a Bean function as an argument to a function in JavaScript?

1

I would like your help. I am developing a system in JSF, and I have the following need: I would like to pass a bean function as an argument to a function of mine in JS, so that I determined future moment I can call such a function. For example:

..Server Side..
class oi{
void diga ola(){}
}
....Xhtml Page....
function foo(f){}
foo(#{oi.ola()});
.....

Oh, you could tell me to use the "p: remoteCommand" which is a component of primefaces, though the code would look ridiculously large. I have already noticed that it generates a function called "PrimeFaces.ab (...)", but unfortunately I did not understand how it recognizes the function that should be called and I have not found a documentation that shows how it works.

What I want to do is a dialog where the content is generic, and this functionality already works. However, I'd like to be able to call a bean function when the dialog is closed so it's hj:

<p:menuitem action="#{função quando abre dialog}" oncomplete="wizardComponent.openDialog('título do dialog,'conteudododialog.xhtml')" value="texto apresentado no menu "/>

In case I wanted to pass another argument to function, like this:

openDialog("título do dialog,'conteudododialog.xhtml', #{função a ser chamada quando o dialog fechar})

Any suggestions are welcome.

    
asked by anonymous 02.08.2016 / 04:33

2 answers

0

Thaylon, specify a listener via an ajax event:

<p:dialog id="suaDialog" widgetVar="suaDialog" header="Sua Dialog"       
          resizable="false" showEffect="clip" hideEffect="clip">
    <p:ajax event="close" update="growl" listener="#{dialogBean.suaFuncaoDeFechamento}" />

    <!-- Conteúdo da dialog -->
</p:dialog>

It's worth taking a look at PrimeFaces ShowCase .

    
02.08.2016 / 14:01
0

I do not know how to do the function pass through parameters, but I needed to do something to manage dialogs in a standard way similar to what you want and worked the other way, see if it suits your project:

In the main XHTML: I make a dialog include, I pass as a parameter a variable output and componentesParaAtualizar (which is accepted by EL - I could not pass a function via param) and I usually call the dialog - PF('widgetVarDialog').show() .

<ui:include src="../dialog/dialog.xhtml">
    <ui:param name="output" value="#{bean.dialogFechada}" />
    <ui:param name="componentesParaAtualizar" value="idComponente1" />
</ui:include>

In dialog.xhtml: The dialog has a close link that calls a fecharDialog() bean function. If it were static, it would be enough to modify this function that everything would be solved, but since what I'm going to do is variable, I use setPropertyActionListener to simulate an event:

<p:commandLink title="Fechar" action="#{bean.fecharDialog()}" ajax="true" update="#{componentesParaAtualizar}" process="@this">
    <f:setPropertyActionListener value="#{true}" target="#{output}" />
</p:commandLink>

In the bean: The variable output that I passed as a parameter has an extra method in setDialogFechada() that does what I need. In this way, the dialog is generic and when it is closed, it executes the method I need.

private boolean dialogFechada;

public boolean isDialogFechada() {
    return this.dialogFechada;
}

public void setDialogFechada(boolean dialogFechada) {
    this.dialogFechada = dialogFechada;
    this.metodoAExecutarQuandoFecharADialog();
}

private void metodoAExecutarQuandoFecharADialog() {
    // Método a executar
}

public void fecharDialog() {
  RequestContext.getCurrentInstance().execute("PF('widgetVarDialog').hide();");
}
    
21.12.2016 / 13:11