How to ensure custom serverListener events fires before action events

Posted by frank.nimphius on Oracle Blogs See other posts from Oracle Blogs or by frank.nimphius
Published on Sat, 26 Feb 2011 13:13:00 +0000 Indexed on 2011/02/26 15:29 UTC
Read the original article Hit count: 367

Filed under:
|

Using JavaScript in ADF Faces you can queue custom events defined by an af:serverListener tag. If the custom event however is queued from an af:clientListener on a command component, then the command component's action and action listener methods fire before the queued custom event. If you have a use case, for example in combination with client side integration of 3rd party technologies like HTML, Applets or similar, then you want to change the order of execution.

The way to change the execution order is to invoke the command item action from the client event method that handles the custom event propagated by the af:serverListener tag. The following four steps ensure your successful doing this

1.       Call cancel() on the event object passed to the client JavaScript function invoked by the af:clientListener tag

2.       Call the custom event as an immediate action by setting the last argument in the custom event call to true

function invokeCustomEvent(evt){
  evt.cancel();       
  var custEvent = new AdfCustomEvent(
                        evt.getSource(),
                        "mycustomevent",                                                                                           
                        {message:"Hello World"},
                        true);
   custEvent.queue();
}

3.       When handling the custom event on the server, lookup the command item, for example a button, to queue its action event. This way you simulate a user clicking the button. Use the following code

ActionEvent event = new ActionEvent(component);
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();

The component reference needs to be changed with the handle to the command item which action method you want to execute.

4.       If the command component has behavior tags, like af:fileDownloadActionListener, or af:setPropertyListener, defined, then these are also executed when the action event is queued. However, behavior tags, like the file download action listener, may require a full page refresh to be issued to work, in which case the custom event cannot be issued as a partial refresh.

File download action tag:
http://download.oracle.com/docs/cd/E17904_01/apirefs.1111/e12419/tagdoc/af_fileDownloadActionListener.html

" Since file downloads must be processed with an ordinary request - not XMLHttp AJAX requests - this tag forces partialSubmit to be false on the parent component, if it supports that attribute."

To issue a custom event as a non-partial submit, the previously shown sample code would need to be changed as shown below

function invokeCustomEvent(evt){
  evt.cancel();       
  var custEvent = new AdfCustomEvent(
                        evt.getSource(),
                        "mycustomevent",                                                                                           
                        {message:"Hello World"},
                        true);
   custEvent.queue(false);
}

To learn more about custom events and the af:serverListener, please refer to the tag documentation:

http://download.oracle.com/docs/cd/E17904_01/apirefs.1111/e12419/tagdoc/af_serverListener.html

© Oracle Blogs or respective owner

Related posts about ADFv

Related posts about JavaScript