Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces onclick and onsuccess differences

Tags:

jsf

primefaces

I have the following situation:

after clicking a button, some business logic is done and after it is done, a new tab with a report should be visible.

<p:commandButton value="this button" update="growlMain"
                 actionListener="#{myBean.businesslogic}" 
                 onstart="ajaxDialog.show();"
                 oncomplete="ajaxDialog.hide();"
                 onsuccess="window.open('./report.jsp', '_newtab');" />

This does not work :(

If the business logic only lasts some milliseconds, the following works:

<p:commandButton value="this button" update="growlMain"
                 actionListener="#{myBean.fastbusinesslogic}" 
                 onclick="window.open('./report.jsp', '_newtab');" />

the onclick opens a new tab, also things like onstart but it doesn't work with onsuccess or oncomplete. Why? And is there a solution for business logic that lasts some seconds?

like image 315
Ziagl Avatar asked Sep 13 '11 12:09

Ziagl


3 Answers

onclick is called before the ajax request is even created (pure client side) while oncomplete and onsuccess are executed after the server responded to the ajax request. So, if you need to execute some business logic before showing a dialog, for example, you want to go with oncomplete. That's what I always use.

You can also condition your javascript inside oncomplete to perform only if there's no validation errors. Intuitively I think onsuccess would behave like that and only execute when there's no validation errors, but that's not how it goes. I don't really know the difference between them. I assume there's a way to flag success=false in the backing beans, but I couldn't really find it in the documentation.

If you want to check for validation in your oncomplete attribute, this is how you'd do:

oncomplete="if (!args.validationFailed){someDialog.hide()}"

In this case, you would only close the dialog if the fields are properly validated. You can actually inject parameters from the backing bean and use them in the javascript after the request has been served. In your backing bean you can do something like this:

RequestContext.getCurrentInstance().addCallbackParam("showDialog", false);

And you can access the parameter like this in your incomplete attribute:

oncomplete="if (args &amp;&amp; args.showDialog){someDialog.show()}else{ alert('the bean didnt let me open the dialog')}"

Anyway, I hope that helps.

like image 84
Andre Avatar answered Nov 17 '22 09:11

Andre


I have noticed that onsuccess for PrimeFaces command button does not work. The oncomplete however works and does the needful even if there is an error , such as in my case shows a success dialog even if there is an error in my business logic. Tried to use onsuccess but doesn't work. You could try oncomplete as below:

<p:commandButton value="this button" update="growlMain"
                 actionListener="#{myBean.businesslogic}" 
                 onstart="ajaxDialog.show();"
                 oncomplete="ajaxDialog.hide(); window.open('./report.jsp', '_newtab');"/>
like image 39
Gunjan Kalra Avatar answered Nov 17 '22 09:11

Gunjan Kalra


You can see the difference here:

Primefaces and ajax onsuccess event

or with onsuccess you can do something before full loading DOM

like image 1
makkasi Avatar answered Nov 17 '22 09:11

makkasi