2 Replies Latest reply on Dec 12, 2007 3:03 PM by indyjones

    Update Data Model from another Bean

      I have a Data Model and some input boxes on a page.

      Select a record from the Data Model and the input boxes is populated with the records information.

      Change the data in the input boxes and hit a "Update" button.

      Update the record in the database and then reload the Data Model.

      All this works great except the reloading of the Data Model...

      Here is the code...

      Bean Responsible for the Data Model

      
      @Name("getusers")
      @Scope(PAGE)
      @Restrict("#{identity.loggedIn}")
      public class GetUsersAction {
      
      
       @DataModel
       private List<User> users2;
      
       @Factory("users2")
       public List<User> getUsers2() { ... }
      
      
      }
      
      


      Bean Responsible for updating the record...

      
      @Name("updateuser")
      @Stateful
      @Scope(EVENT)
      @Restrict("#{identity.loggedIn}")
      public class UpdateUserAction implements UpdateUserActionInterface {
      
      
       public void updateUser() { ... }
      
      }
      
      


      And the web page...

      
      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
       xmlns:s="http://jboss.com/products/seam/taglib"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:a4j="http://richfaces.org/a4j"
       xmlns:rich="http://richfaces.org/rich"
       template="layout/template.xhtml">
      
       <ui:define name="body">
      
       <h:form>
      
       <a4j:region>
      
       <rich:dataTable id="tblUsers" var="varUser" value="#{users2}" >
      
       ....
      
       </rich:dataTable>
      
       </a4j:region>
      
       <a4j:commandButton value="Update" action="#{updateuser.updateUser()}" ></a4j:commandButton>
      
       <h:inputText value="#{currentUser.firstname}"></h:inputText>
      
       </h:form>
      
       </ui:define>
      
      </ui:cocmposition>
      
      


      The question is, how do I update the Data Model (users2) from the updateuser Bean after the updateUser method has completed?

      I don't want to Inject and Outject it....

      Thanks

      indy

        • 1. Re: Update Data Model from another Bean

          You could use the event model provided by Seam. In the updateuser bean simply raise an event:

          ...
          public void updateUser() {
           ...
           Events.instance().raiseEvent("userUpdated", user);
          }
          ...


          Listen for that event in a method in your getusers component:

          ...
          @Observer("userUpdated")
          public void updateDataModel(User user) {
           // update the associated user in users2
          }
          ...


          Another option if you don't want to pass the user that was updated through the event, you could simply re-execute the factory method and use the @RaiseEvent annotation on the updateUser() method. Hope that helps.

          • 2. Re: Update Data Model from another Bean

            Very nice...

            I am new to the Events model...

            Thanks!!!

            Indy