3 Replies Latest reply on Apr 6, 2013 10:37 AM by f.sauter

    Errai UI - Singleton composite in page layout using Errai Navigation

    f.sauter

      Hi all,

       

      according the documentation (https://docs.jboss.org/author/display/ERRAI/Errai+UI#ErraiUI-ExtendCompositecomponents) I defined a PageLayout and extended all my Page composites.

       

       

      {code:java}
      @Templated
      public class PageLayout extends Composite {
      
         @Inject
         @DataField
         private SideBarComponent sidebar;
      
      }
      
      @Dependent
      @Page(startingPage = true)
      @Templated("PageLayout.html")
      public class HomePageLayout extends PageLayout {
                @Inject
                @DataField
                private HomePage content;
      }
      
      @Templated
      public class SideBarComponent extends Composite {
      
         private List transactions;
      
            @Inject
            private Caller transactionListService;
      
            @AfterInitialization
             private void init() {
                 // load transactions and change dom...
             }
      }
      //... and so on
      {code}

       

      Works like a charm, but after following a link, the SideBarComponent is going to lose all it's fetched data. Now the question: What's the best way (practise) to declare the SidebarComponent as Singleton? Only adding @Singleton isn't working for me.. should it?

       

      Cheers Flori

        • 1. Re: Errai UI - Singleton composite in page layout using Errai Navigation
          jfuerth

          Hi Flori,

           

          Yes, either @Singleton or @ApplicationScoped on your SideBarComponent should be sufficient. The code that makes the instance looks like this:

           

                public void produceContent(CreationalCallback callback) {
                  bm.lookupBean(YourPageClass.class).getInstance(callback);
                }
          

           

          The bm variable is the Errai client-side AsyncBeanManager, inherited from NavigationGraph. You can double-check this code in your own project in .errai/GeneratedNavigationGraph.java.

           

          Is it possible something old is stuck in dev mode? Have you tried stopping dev mode and cleaning your project?

           

          -Jonathan

          • 2. Re: Errai UI - Singleton composite in page layout using Errai Navigation
            jfuerth

            Oh wait!

             

            What I said in my previous post is only true for @Page classes. I believe that when a template is being composited, the @Injected templated beans are always newly instantiated regardless of their scope.

             

            Probably your best bet would be to keep that state in a separate @ApplicationScoped bean and @Inject that bean into SideBarComponent:

             

            @ApplicationScoped
            public class SideBarTransactions {
              private List<Transaction> transactions;
              @AfterInitialization
              private void init() {
                // load transactions
              }
              public List<Transaction> get() {
                return transactions;
               }
            }
            

             

            Hope that helps!

             

            -Jonathan

            1 of 1 people found this helpful
            • 3. Re: Errai UI - Singleton composite in page layout using Errai Navigation
              f.sauter

              Thanks Jonathan, I'll use the seperate bean for now but it would be really nice to have the possibility to declare a singleton composite for page layouts like in GWTP a singleton PresenterWidget.

               

              BTW: Here is the stacktrace which occurs if I try to annotate my SideBarComponent with @Singleton or @ApplicationScoped (Errai 2.3.0.CR1): http://pastebin.com/rFptVe0e