I’d like to refresh a dynamically when new Elements has been created.
I came around 3 solutions, and like to know which is best and to prefer.
Or rather: I like nr. 3 (ajax) most, but unfortunatelly this is the one not working. And I wonder how I could make it work.
a). with @Observes:
public class Facade {
private List<Customer> customerList;
public void createNew() {
service.create(newCustomer);
event.fire(newCustomer);
}
public void onCustomerChanged(
@Observes(notifyObserver = Reception.IF_EXISTS) final Customer newCustomer) {
findAll();
}
@PostConstruct
public void findAll() {
customerList = service.findByNamedQuery("Customer.ALL");
}
public List<Customer> getCustomerList() {
return customerList;
}
}
b). just call the findAll update method directly in the dontect of createNew:
public void createNew() {
service.create(newCustomer);
findAll();
}
c). with Ajax:
public void createNew() {
service.create(newCustomer);
}
<h:form>
<h:commandButton id="register" action="#{facade.createNew()}"
value="Register">
<f:ajax execute="@form" render=":customerTable" />
</h:commandBUtton>
</h:form>
<h:datatable id="customerTable" var="_customer" value="facade.customerList">
Unfortunatelly the ajax thing does work delayed: eg create customer1 > nothing happens. create cust2 > datatable refreshes and displays cust1. and so on.
How could I fix that?
Thanks a lot
You need to reload the list after adding new entry, exactly as you did in b).