0

I need ideas to solve next situation.

I have a commandButton with ajax that invokes a listener with a boolean method, something like this:

<h:commandButton value="Add">
    <f:ajax listener="#{shoppingCartBean.makeReservation(devicesBean.device)}" />
</h:commandButton>

If the listener returns true, its because device is correctly reserved, everything works fine and I need to redirect to another url with a location.href js function.

If the listener returns false, I need that a js alert will be shown with a "Sorry, no device stock bla bla bla".

How can I manage that? Thx in advance.

dcalap
  • 1,048
  • 2
  • 13
  • 37

1 Answers1

0

You can use JQuery:

<h:commandButton id="someId" action="#{myBean.myAction}">
       <f:ajax onevent="myJsFunction"   render="whatevet"></f:ajax>
</h:commandButton>
function myJsFunction(data) {
   if (data.status === 'success') {
     alert('complete');
   }
}

for more detail, see this track https://stackoverflow.com/a/15806084/3056912

Community
  • 1
  • 1
xild
  • 187
  • 8
  • But the problem is the js function needs the listener (in my case) or the action (in yours) boolean result to know what to do... I don't know if you understand what I mean. In your example, if myBean.myAction returns true, the myJsFunction needs this result to alert or do another thing... – dcalap Jan 17 '14 at 10:35
  • hmm, why dont you use navigation rule? if true you do the redirect. If not you set a boolean and rerender the popup component based on the boolean... – xild Jan 17 '14 at 11:05
  • Its because of pretty-faces I need to redirect with an URL not with an xhtml resource.... but thx. – dcalap Jan 17 '14 at 12:25
  • hmmm :/ If you use primefaces you can use onComplete. ` ` – xild Jan 17 '14 at 12:57
  • hehe I think u haven't readed it correctly, not prime, PRETTY-FACES, for urls :) – dcalap Jan 17 '14 at 12:59
  • Yeah... but if you have primeface in your project... you can use oncomplete attribute to do what you want... I think your problem is more logical than a limitation by jsf/pretty-faces. Buuuut... i tried :D – xild Jan 17 '14 at 13:17
  • Thx 4 everything, but unfortunately I can't use PF in this project... :( – dcalap Jan 20 '14 at 12:28
  • :( Man take a look: http://stackoverflow.com/a/3718859/3056912 maybe you can use this with my suggestion bellow to do your redirect with pretty-faces, no? :} – xild Jan 20 '14 at 12:47