0

How can I access a backing Bean property from a callback JavaScript function which is executed after an AJAX request has been successfully completed?

My .xhtml code is the following:

<h:commandLink value="Link" >
    <f:ajax event="action" listener="#{user.onClickLink}" onevent="onEventLink"/>
</h:commandLink>

My .js function code is the following:

function onEventLink(data){
    switch (data.status) {
        ...
    case "success":
    // here i need to get a backing Bean property
    break;
    }
}

I know I can pass the backing Bean property to the JS function by using an EL doing something like this:

onevent='onEventLink(#{user.getProperty()})'

but doing that I loose the reference to the data object JSF passes to the function related to 'onevent' property.

I'd really appreciate if someone could tell me the rigth way to acomplish such sort of task. Thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
txapeldot
  • 71
  • 2
  • 10
  • Which one is most helpful in solving your concrete problem? http://stackoverflow.com/q/9067685 or http://stackoverflow.com/a/31154046? – BalusC Feb 25 '16 at 16:51
  • Certainly, the first one. Thanks a lot! – txapeldot Feb 25 '16 at 16:57
  • I'm affraid that the solution provided in the first post is not valid to sort out the need I have: acessing to a backing bean property once the AJAX request has been completed. – txapeldot Feb 26 '16 at 10:55
  • Because if I define onevent='function (data) { onEventLink(data, "#{user.getmethod()}")}', the bean property is passed to the inner function as a constant value for each AJAX state, regardless of the actual value that the bean property could have as a consecuence of the, for instance, the execution of a listener method. – txapeldot Feb 26 '16 at 11:03
  • And my need is to get the actual value of the bean property (#{user.getmethod()}) once the listener method has finished its execution. And I'm supposing the AJAX-complete state is fired once the listener method has been completely finished. – txapeldot Feb 26 '16 at 11:09
  • Then you actually needed the solution in second link (as I initially guessed). – BalusC Feb 26 '16 at 11:10
  • Could you please confirm to me that the AJAX-complete state is fired once the backing listener method has finished its execution? – txapeldot Feb 26 '16 at 11:23
  • Yes. See also `onevent` example here http://stackoverflow.com/q/13081129 – BalusC Feb 26 '16 at 11:26
  • Then, isn't there a way of calling a backing bean property from the JS function intended to manage the AJAX 'complete' status? – txapeldot Feb 26 '16 at 11:32
  • Just let JSF print it as if it's a JS argument/variable. – BalusC Feb 26 '16 at 11:33
  • Print what?? I didn't catch it. An example, please.... :( – txapeldot Feb 26 '16 at 11:38
  • As shown in "standard JSF" example in the 2nd link in 1st comment. – BalusC Feb 26 '16 at 11:51
  • Thanks for your help. The solution provided in the second link fulfills my requeriments. Let me ask you something: why is it needed to call 'someFunction()' within the 'handleAjax function'? As you said, "...this way it's executed as the page loads and also if the form get updated by ajax." Since the form gets updated by ajax, I don't think it's required to call 'someFunction()' when 'success' state is fired. – txapeldot Feb 26 '16 at 13:55
  • Indeed. The `onevent` becomes unnecessary. You just control it with `render`. – BalusC Feb 26 '16 at 13:56
  • And moreover, the form gets updated after the 'complete' state is fired and before 'success' state is fired. – txapeldot Feb 26 '16 at 13:57

0 Answers0