1 Reply Latest reply on Dec 24, 2013 9:51 AM by jfuerth

    CDI Events Across Errai UI Pages

    chaluwa

      I cannot seem to fire and observe CDI events between one @Templated @Page and another, but observing the CDI event within the same page component works. I mean, CDI event fired in @Template 1 does not trigger its @Observes method in @Template 2, but only the @Observes method in the source @Template 1 is triggered ? What am I missing.

       

      @Portable
      public class DataAvailable{
           ....
      }
      
      ...
      
      @Page(path="putmesubjectbundles")
      @Templated("putmesubjectsbundles.html")
      public class PutmeSubjectBundles {
      
           @Inject @ClientSide
           private Event<DataAvailable> dataEvt;
      
           
           ..
           private void trigger(){
                dataEvt.fire(new DataAvailable());
           }
      
           private void onDataAvailable(@Observes @ClientSide DataAvailable evt){
                LogUtil.log("Bundles onDataAvailable";     // executes
           }
      }
      
      @Page(path = "putmesubjects")
      @Templated("putmesubjectsbundles.html")
      public class PutmeSubjects{
           ...
           public void onBundlesAvailable(@Observes @ClientSide DataAvailable evt){
                LogUtil.log("Subjects onBundlesAvailable";     // fails to execute
           }
      }
      
      
      
        • 1. Re: CDI Events Across Errai UI Pages
          jfuerth

          Hi Charles,

           

          On the client side, Errai's CDI events are delivered to every live object with a matching observer method. In your sample code, you're not declaring a scope on your @Page objects, so they have the default @Dependent scope. This means that only the currently-visible page is live at any given time.

           

          If you want both @Page classes to receive events even when they're not visible, add the @ApplicationScoped annotation to them.

           

          You might also consider factoring out the common state between the two pages into a single @ApplicationScoped bean which is only concerned with state management (rather than a combination of state management and UI). It could still fire events when important changes happen, but then perhaps only the currently-visible page would need to receive the events. Then you can go back to default-scoped pages and hopefully enjoy a more maintainable codebase. :-)

           

          -Jonathan