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

Errai UI - Singleton composite in page layout using Errai Navigation

f.sauter Newbie

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.

 

 

@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

 

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
    Jonathan Fuerth Master

    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
    Jonathan Fuerth Master

    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

  • 3. Re: Errai UI - Singleton composite in page layout using Errai Navigation
    f.sauter Newbie

    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