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

Asked

Viewed 836 times

2

I’d 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 from a function of mine in JS, so that I certain 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()});
.....

Ah, you could tell me to use the "p:remoteCommand" which is a component of the primefaces, however the code would get ridiculously large. I have observed that it generates a function called "Primefaces.ab(...)", but unfortunately I did not understand how it recognizes the function that should be called and did not find a documentation that shows its functioning.

What I want to do is a dialog where the content is generic, and this functionality already works. However, I would like to have the possibility of calling a bean function when the dialog is closed so is 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, so:

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

Any suggestion is welcome.

2 answers

0

Thaylon, specify a Listener through an event ajax:

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

Worth a look at Primefaces Showcase.

  • Hello, thanks for the suggestion. Getting the closing event is not my problem. What I want to pass is the #{dialogBean.suaFuncaoDeechamento} as an argument of a javascript function of mine, because I manage the dialog by JS, and in case I want the solution to be generic.

  • Have you seen how the PrimeFaces.ab, which is a shortcut to PrimeFaces.ajax.AjaxRequest? Example: PrimeFaces.ab({formId:'dialogForm',source:'dialogForm:componenteQualquerId',process:'@all',update:'growl'}) With it, you can call your methods on bean via Javascript.

0

I don’t know how you do the passing of function by parameters, but I needed to do something to manage dialogs in a standardized way similar to what you want and worked by another way, see if it serves your project:

In the main XHTML: I do a dialog include, step as parameter a variable output and componentesParaAtualizar (which is accepted by EL - I couldn’t 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 the dialog.xhtml: The dialog has a close link that calls a bean function fecharDialog(). If it were something static, it would be enough to modify this function that everything would be solved, but as what I will do is variable, I use the 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>

No bean: The variable output I passed as parameter has an extra method in setDialogFechada() which does what I need. That 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();");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.