[OmniFaces utilities] The
subscribeToApplicationEvent()
methods subscribes the given callback (serializable (in 2.1) /un-serializable (in 2.0) void without arguments/void with one argument) to the current application that get invoked every time when the given system event type is published in the current application.Method:
- using OmniFaces 2.1 serializable callback
- using OmniFaces 2.0 un-serializable callback
Note By OmniFaces implementation, the listened emitter of system events is the UIViewRoot, so use only system events types that can be emitted by the component tree's view root. OmniFaces relies on its org.omnifaces.eventlistener.DefaultViewEventListener, listed below:
public
abstract class DefaultViewEventListener implements SystemEventListener {
@Override
public void processEvent(SystemEvent event)
throws AbortProcessingException {
// NOOP
}
@Override
public boolean isListenerForSource(Object
source) {
return
source instanceof UIViewRoot;
}
}
Usage:
Before
calling the Events#subscribeToApplicationEvent()
methods, let's have a quick overview of how this methods works. As the post
title said, this method allows to subscribe the given serializable (in 2.1)/un-serializable (in 2.0) callback to the current
application that get invoked every time when the given system event type is
published in the current application. So, we pass to this method the system
event type to be observed, and one of the following OmniFaces Callbacks
(this becomes the listener):
- using OmniFaces 2.1 serializable callback
- using OmniFaces 2.0 un-serializable callback
- using OmniFaces 2.1 serializable callback
- using OmniFaces 2.0 un-serializable callback
Basically, all we need to do is to subscribe to the desired event(s) with a Callback instance and implement a behavior to the corresponding invoke() method. Behind the scene, OmniFaces takes the provided Callback and creates a custom system event listener for it. When the specified event(s) occurs, OmniFaces will call our invoke() implementation. Knowing this, we can create a simple example. Let's suppose that we have a custom component that needs to know when the view root was just added to the view. For this, the custom component can subscribe with a Callback to the PostAddToViewEvent event, as below:
import
org.omnifaces.util.Callback;
import
static org.omnifaces.util.Events.subscribeToApplicationEvent;
...
//OmniFaces 2.1
//OmniFaces 2.0
//OmniFaces 2.1
// serializable void Callback with argument
subscribeToApplicationEvent(PostAddToViewEvent.class, new Callback.SerializableWithArgument<SystemEvent>() {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
@Override
public void invoke(SystemEvent event) {
System.out.println("..: PostAddToViewEvent event emitted by UIViewRoot :.." + event.getSource());
//do something ...
}
});
//OmniFaces 2.0
// un-serializable void
Callback with argument
subscribeToApplicationEvent(PostAddToViewEvent.class,
new Callback.WithArgument<SystemEvent>()
{
@Override
public void invoke(SystemEvent event) {
System.out.println("..:
PostAddToViewEvent event emitted by UIViewRoot :.." + event.getSource());
//do something ...
}
});
// OmniFaces 2.1
// serializable void Callback
subscribeToApplicationEvent(PostAddToViewEvent.class, new Callback.SerializableVoid() {
@Override
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
public void invoke() {
System.out.println("..: PostAddToViewEvent event emitted by UIViewRoot :..");
//do something ...
}
});
// un-serializable void
Callback
subscribeToApplicationEvent(PostAddToViewEvent.class,
new Callback.Void() {
@Override
public void invoke() {
System.out.println("..:
PostAddToViewEvent event emitted by UIViewRoot :..");
//do
something ...
}
});
...
Niciun comentariu :
Trimiteți un comentariu