AutoUpdate allows you to use the observer pattern in which a component maintains a list of its dependents, called observers, and notifies them automatically of any state changes. Instead of having to list every component you want to update on a CommandButton you can subscribe components to listen to that button's event using @obs(event).
<div class="card">
<h:form>
<h:outputLabel for="name" value="Name" styleClass="mr-2" />
<p:inputText id="name" value="#{observerView.text}">
<p:ajax process="@this" update="@none" ignoreAutoUpdate="true"/>
</p:inputText>
<div class="my-3">
<p:commandButton value="Update global" icon="pi pi-check" styleClass="mr-2" />
<p:commandButton value="Update event1" update="@obs(event1)" icon="pi pi-check" styleClass="mr-2"/>
<p:commandButton value="Update event2" update="@obs(event2)" icon="pi pi-check"/>
</div>
<h:outputText id="displayGlobal" value="Global: #{observerView.text}" styleClass="block mb-3">
<p:autoUpdate/>
</h:outputText>
<h:outputText id="displayEvent1" value="Event1: #{observerView.text}" styleClass="block mb-3">
<p:autoUpdate on="event1"/>
</h:outputText>
<h:outputText id="displayEvent2" value="Event2: #{observerView.text}" styleClass="block mb-3">
<p:autoUpdate on="event2"/>
</h:outputText>
</h:form>
</div>
package org.primefaces.showcase.view.ajax;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import java.io.Serializable;
import io.quarkus.runtime.annotations.RegisterForReflection;
@Named
@ViewScoped
@RegisterForReflection(serialization = true)
public class ObserverView implements Serializable {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}